c# - Trying to make calculated properties play nice with Entity Framework -



c# - Trying to make calculated properties play nice with Entity Framework -

here's simplified version of classes i'm working with:

public class parent { public int id { get; set; } public list<child> children { get; set; } public int childrensum { { homecoming children.sum(c => c.value); } } } public class kid { public int id { get; set; } public int value { get; set; } public parent parent { get; set; } } public class testdbcontext : dbcontext { public dbset<parent> parents { get; set; } public dbset<child> children { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<child>().hasrequired(e => e.parent); modelbuilder.entity<parent>().hasmany(e => e.children); } } public class testdbcontextinitializer : dropcreatedatabasealways<testdbcontext> { protected override void seed(testdbcontext context) { var parent = new parent { children = new list<child>() }; parent.children.add(new kid { value = 3 }); parent.children.add(new kid { value = 1 }); parent.children.add(new kid { value = 2 }); context.parents.add(parent); } }

when running, seed info in database, childrensum property fails out because children aren't beingness eager loaded. expectation be, since didn't create navigation properties virtual. missing something?

when create navigation property virtual enable lazy loading. you've got right. opposite of lazy loading not eager loading in case. "no loading unless you loading".

so either have enable lazy loading or utilize include. or like

db.entry(parent).collection(p => p.children).load();

where db testdbcontext instance , parent fetched parent object.

c# entity-framework

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -