Entity Framework 6 One-To-Many Relationship without Navigation Property

c# ef-code-first entity-framework-6

Question

I have two tables, A and B, with a one-to-many relationship. EF 6 was used to implement these tables as follows:

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

once I recoverB based on contextInstanceOfClassA constant null

Assumption

Due to the lack of a navigation attribute,B in A Entity Framework does not lazily load as a result of entity.A after retrievalB .

Expecting

Since I don't require accessB from A , hence I'd like to remove the navigation property inA but keep the capacity for lazy load.A from B .

NOTE

I observed a post from Relationship from many to many maps without navigation, however it isn't applicable to my situation.

Is it possible for me to force a sluggish load?A from B without making explicit use ofvar b = context.B.Include(x => x.InstanceOfClassA); ? Perhaps Customary Convention

EDIT 1

I tried the following convention but was unsuccessful:

modelbuilder.Entity<B>()
    .HasRequired<A>(x => x.InstanceOfClassA);
1
4
5/23/2017 12:08:04 PM

Accepted Answer

You must mark navigation property with in order to get lazy loading to function.virtual keyword:

[ForeignKey("AID")]
public virtual A InstanceOfClassA {get;set;}
1
3/7/2014 5:27:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow