Entity Framework 6 One-to-Many Relationships Without Navigation Property

I have 2 tables: A and B with a one-to-many relationship, this table was implemented in EF 6, as shown below:

public class A
{
    [Key]
    public int AID {get;set;}
    public string AName {get;set;}
}

public class B
{
    [Key]
    public int BID {get;set;}
    public string BName {get;set;}
    public int AID {get;set;}

    [ForeignKey("AID")]
    public A InstanceOfClassA {get;set;}
}

PROBLEM

When I extract Bfrom the context, InstanceOfClassAalways null.

Assumption

Since the navigation property does not apply to the object Bin A, therefore, the entity infrastructure does not have lazy loading Awhen retrieving B.

Awaiting

Since I do not need to access Bfrom A, I therefore want to get rid of the navigation property in A, but at the same time retain the ability of lazy loading Afrom B.

Note

"-" , .

A B include var b = context.B.Include(x => x.InstanceOfClassA);? ,

1

, , :

modelbuilder.Entity<B>()
    .HasRequired<A>(x => x.InstanceOfClassA);
+4
1

, virtual:

[ForeignKey("AID")]
public virtual A InstanceOfClassA {get;set;}
+1

Source: https://habr.com/ru/post/1530529/


All Articles