简体   繁体   English

EF代码优先如何使用Entity检索导航属性?

[英]EF code-first how to retrieve navigation-property with Entity?

Following is a data model wherein Entity A has a navigation-property B 以下是一个数据模型,其中实体A具有导航属性B

[Serializable]
public class X
{   
    [Key]
    public int Id { get; set; }             
}

[Serializable]
public class A : X
{     
    public List<B> B { get; set; }
}

[Serializable]
public class B : X
{
    public int AId { get; set; }
    public A A { get; set; }
}

Then, I'm trying to access table A from database as follows : 然后,我尝试从数据库访问表A,如下所示:

public class MyDatabaseContext : DbContext
{
    Configuration.ProxyCreationEnabled = false;
}

public async Task<IEnumerable<T>> SelectAsync<T>() where T : X
{
    using (MyDatabaseContext db = new MyDatabaseContext ())
    {
        var dataTable = db.Set<T>();
        var temp = await dataTable.ToListAsync();

        return temp;
    }
}

This almost works except that for each instance of A returned in the collection temp the value of B is null . 除了对于集合temp返回的A每个实例, B值为null之外,这几乎可以工作。 Since each A has a non null B in the database I'm surprised with the result of db.Set<T>() 由于每个A在数据库中都有一个非null B ,我对db.Set<T>()的结果感到惊讶

Questions : 问题

  • Why am I getting this result? 为什么我得到这个结果?
  • What do I need to do for db.Set<T>() to return A s with valid (non-null) B s? 对于db.Set<T>()以返回具有有效(非空) BA我需要做什么?

Update Based on suggestion of @Camilo Terevinto I tried the following, without luck 更新基于@Camilo Terevinto的建议,我尝试了以下操作,但没有运气

public class MyDatabaseContext : DbContext
{
    Configuration.LazyLoadingEnabled = true;
}

Since you are explicitly disposing the context and you disabled lazy loading, you need to eagerly load the relation: 由于您要显式处理上下文,并且禁用了延迟加载,因此需要急于加载该关系:

await dataTable.Include("B").ToListAsync();

You will need to decide how you determine whether the current T contains a navigational B though. 您将需要决定如何确定当前T是否包含导航B

I could not say why you get null value. 我不能说为什么你得到null值。 Normally, here is how one-to-one are configured in Entity Framework. 通常,这是在实体框架中one-to-one配置的方式。

When runing the following code with your SelectAsync method, I get B for every value of A . 当使用SelectAsync方法运行以下代码时,对于每个A值,我都会得到B

public partial class X
{
    public int Id { get; set; }

    public virtual A A { get; set; }

    public virtual B B { get; set; }
}

public partial class A
{
    public A()
    {
        B = new HashSet<B>();
    }

    public int Id { get; set; }

    public virtual X X { get; set; }

    public virtual ICollection<B> B { get; set; }
}

public partial class B
{
    public int Id { get; set; }

    public int? AId { get; set; }

    public virtual A A { get; set; }

    public virtual X X { get; set; }
}

DbContext DbContext

public partial class MyDatabaseContext : DbContext
{
    ...

    public virtual DbSet<A> A { get; set; }
    public virtual DbSet<B> B { get; set; }
    public virtual DbSet<X> X { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<X>()
            .HasOptional(e => e.A)
            .WithRequired(e => e.X);

        modelBuilder.Entity<X>()
            .HasOptional(e => e.B)
            .WithRequired(e => e.X);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先 IQueryable 导航属性 - Entity Framework code-first IQueryable navigation property 空导航在EF 5代码优先 - Null Navigation In EF 5 code-first 如何在父实体中拥有一个属性,该父实体的值属于子实体,而EF6和Code-First则将其下两层 - How to have a property in a parent entity whose value belongs to a child entity two levels down with EF6 and Code-First 具有空导航属性EF Code的新Poco实体 - New Poco Entity with null navigation property EF Code first 在EF4代码优先中,如何使一个实体的主键成为另一个实体? - How can I have the primary key of one entity be another entity in EF4 code-first? EF Code-First:具有单向导航的多对多 - EF Code-First: Many-to-many with one direction navigation EF4.1代码优先:如何在依赖实体中禁用没有导航属性的关系的删除级联 - EF4.1 Code First : How to disable delete cascade for a relationship without navigation property in dependent entity 实体框架配置问题(EF Code-First) - Entity Framework Configuration Question (EF Code-First) 影响如何从数据库中以EF 6代码优先创建实体类 - Influencing how entity classes are created in EF 6 code-first from database 使用EF6代码优先,引用相同的属性名称 - Using EF6 code-first, reference same property name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM