c# - Automatically set foreign key in grandchild table in Entity Framework 6 -
say have following objects
public class myparent { public long id { get; set; } public string name { get; set; } } public class mychild { public long parentid; public virtual parent parent; public long id { get; set; } public string name { get; set; } } public class mygrandchild{ public long parentchildid; public virtual mychild parentchild; public long id { get; set; } public string name { get; set; } }
now myparent top-level object, have child of mychild, , mychild has child of mygrandchild.
in tables created, get
myparent --id --name mychild -- parentid -- id -- name mygrandchild -- parentchildid -- id -- name
now useful have foreign key myparent in mygrandchild table.
i have tried changing mygrandchild object this
public class mygrandchild{ public long parentid; public virtual myparent parent; public long parentchildid; public virtual mychild parentchild; public long id { get; set; } public string name { get; set; } }
but error thrown cascase delting, , had add following fluentapi.
modelbuilder<mygrandchild>() .hasrequired(e => e.parent) .withmany() .hasforeignkey(e => e.parentid) .willcascadeondelete(false);
now works, forces manually have set .parent property on mygrandchild.
is there way of telling entity framework want store parent parent foreign key , have set manually?
Comments
Post a Comment