Question

Suppose I have following class

   public class A
{
    public int ID{get;set;}
    public string Name{get;set;}
    public List<B> BList{get;set;}
}
public class B
{
    public int ID{get;set;}
    public int AID{get;set;}
    public string Name{get;set;}
    public A AObject{get;set;}
}
public class AMap:EntityTypeConfiguration<A>
{
   public AMap()
   {
    this.ToTable("ATable");

    this.HasKey(x => x.ID);
   }
}
public class BMap:EntityTypeConfiguration<B>
{
   public BMap()
   {
    this.ToTable("BTable");

    this.HasKey(x => x.ID);

    this.Property(x => x.AID).IsRequired();
    this.HasRequired(x => x.AObject).WithMany(x => x.BList).HasForeignKey(x => x.AID);
   }
}

When I get single records of ATable then it should bind list of BTable records in A object but I am geting BList null.

What am I missing here?

Était-ce utile?

La solution

you should use virtual in navigation.

public class A
{
    public int ID{get;set;}
    public string Name{get;set;}
    public virtual List<B> BList{get;set;}
}
public class B
{
    public int ID{get;set;}
    public int AID{get;set;}
    public string Name{get;set;}
    public virtual A AObject{get;set;}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top