c# - Entity Framework - Add child object to parent -


i'm trying implement simple one-to-many relationship entity framework , code first. set parent class in simple way :

public class parent {     [key]     public long parentid { get; set; }     public icollection<child> child { get; set; }      public parent()     {         child = new hashset<child>();     } } 

this simplified version of course. child class simple parent 1 :

public class child {     [key]     public long childid { get; set; }     public int parentid { get; set; }     public virtual parent parent { get; set; } } 

my controllers ones generated ef. problem when create child parent id in post method, id set in database when i'm getting parent, children list empty.

i've tried adding child in parent in controller :

db.parent.find(child.parentid).child.add(child); 

which apparently had no effect.

the parent in child object null , child list in parent empty.

how can child in parent's collection ?

thank !

as far know, disabling proxy creation affect lazy loading. lazy loading can work within proxy class. if not, ef not have way control when you're accessing properties.

the way can childs use eager loading (using include())


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -