c# - Entity Framework 6 - lazy loading not working -


this first time using ef. such may have missed simple preventing lazy loading of bclass. when load aclass 'b' property null. have expected populated persisted.

for example have 2 simple classes:

public class aclass {     public aclass()     {         id = guid.newguid();         b = new bclass();     }     [key]     [databasegenerated(databasegeneratedoption.none)]     public guid id { get; set; }     public string name { get; set; }     public virtual bclass b { get; set; } }  public class bclass {     public bclass()     {         id = guid.newguid();     }     [key]     [databasegenerated(databasegeneratedoption.none)]     public guid id { get; set; }     public string description { get; set; } } 

in 'context' class:

public dbset<aclass> aclasses { get; set; } public dbset<bclass> bclasses { get; set; } 

below simple test. expecting x.b loaded. instead null?

using (var db = new testcontext()) {     aclass = new aclass();     a.name = "aclass";     a.b.description = "bclass description goes here!";     db.aclasses.add(a);     db.savechanges(); // works. confirmed both , a.b persisted database }  using (var db = new testcontext()) {     aclass x = db.aclasses.where(a => a.name == "aclass").firstordefault();     system.console.writeline(x.name);     system.console.writeline(x.b.description); // x.b null. expected load , .description property "bclass description goes here!" } 

changes persisted database correctly. can't post screen shot yet.

from aclass table:

 id  name    b_id b7937e1b-9cc0-4318-b179-0d54b23b6cda    aclass  560d066b-4848-454d-b92c-f6ae4232057e 

from bclass table:

 id  description 560d066b-4848-454d-b92c-f6ae4232057e    bclass description goes here! 

entity framework version: 6.0.0.0

remove b initialization form class aclass:

public aclass() {     id = guid.newguid();     //b = new bclass(); } 

the initialization causes entity framework think assigned new value b property of aclass , not assign proxy.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -