c# - 使用延迟加载通过实体框架获取实体父级
问题描述
我需要一种解决方案来使用 EF 延迟加载从孩子那里检索父对象。我有 10 个不相关的类可以使用一种类型的子类,因此不知道如何为其设置 LINQ 查询。我看过这里,但这让我觉得我必须知道孩子实际上与 Parent1 相关联。在链接中,我知道博客有帖子,但帖子不会有博客。我没有这种稳固的关系。
这是我的班级设置的示例:
public class Parent1
{
public int Parent1Id { get; set; }
... other properties ...
public virtual Child Child { get; set; }
}
public class Parent2
{
public int Parent2Id { get; set; }
... other properties ...
public virtual Child Child { get; set; }
}
public class Parent3
{
public int Parent3Id { get; set; }
... other properties ...
public virtual Child Child { get; set; }
}
public class Child
{
public int ChildId { get; set; }
... other properties ...
}
孩子已Parent2
通过DbContext
.
在此之后,我检索子对象并想确定它与哪个父类相关联。
编辑
我有一个像这样的通用调用
public T GetById<T>(int id) where T : Base, new()
{
T entity = _Context.Set<T>().Find(id);
return entity;
}
然后我像这样打电话
Child childObj = DB.GetByID<Child>(id);
解决方案
推荐阅读
- python - 如何在python中将字母与10的倍数配对(0A-9A然后0B ....)
- javascript - 在php中计算每月包(产品)价格
- javascript - 从一个位置在操作系统上安装的所有浏览器中运行 JavaScript 脚本
- python - 如何将字符串拆分为变量
- java - 如何在Java矩阵(二维数组)中围绕10乘10 0的矩阵制作1的“边界”
- python - 无法将熊猫数据框附加到现有的 Excel 工作表
- java - Android Studio 项目级 build.gradle 错误
- excel - 宏从 txt 文件中提取不正确的数据
- node.js - 从共享 linux 托管服务器连接到 mongodb Atlas 时出错
- webrtc - 错误:ast_sockaddr_resolve:getaddrinfo(“a783543c-1911-44c4-9ba1-52114bbdccb4.local”,“(null)”,...):名称或服务未知