C# 使用延迟加载获取实体框架的实体父级

C# 使用延迟加载获取实体框架的实体父级,c#,entity-framework-6,lazy-loading,entity-relationship,C#,Entity Framework 6,Lazy Loading,Entity Relationship,我需要一个解决方案来使用EF延迟加载从子对象检索父对象。我有10个不相关的类可以使用一种类型的子类,因此不知道如何为它设置LINQ查询。我看过了,但这让我觉得我必须知道孩子实际上与父母有联系。在链接中,我知道博客有帖子,但帖子不会有博客。我没有这种牢固的关系 以下是我的课堂设置示例: public class Parent1 { public int Parent1Id { get; set; } ... other properties ... public virt

我需要一个解决方案来使用EF延迟加载从子对象检索父对象。我有10个不相关的类可以使用一种类型的子类,因此不知道如何为它设置LINQ查询。我看过了,但这让我觉得我必须知道孩子实际上与父母有联系。在链接中,我知道博客有帖子,但帖子不会有博客。我没有这种牢固的关系

以下是我的课堂设置示例:

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);
public T GetById(int-id),其中T:Base,new()
{
T entity=_Context.Set().Find(id);
返回实体;
}
然后我就这样打电话

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);
Child childObj=DB.GetByID(id);

您能告诉我们您是如何取回孩子的,并演示什么不起作用吗?@Marco在问题中添加了编辑。在得到子对象后,我需要找到它绑定到的父对象。我通过已登录的用户获取子项。