c# - Entity Framework 6 inserts null as primary key while inserting nested identifying relationships -
i have 4 entities defined using ef6 code first:
public class item1 { [key] [stringlength(50)] public string name { get; set; } } public partial class item2 { [key] [column(order = 0)] [stringlength(50)] public string item1name { get; set; } [key] [column(order = 1)] [stringlength(50)] public string name { get; set; } } public partial class item3 { [key] [column(order = 0)] [stringlength(50)] public string item1name { get; set; } [key] [column(order = 1)] [stringlength(50)] public string item2name{ get; set; } [key] [column(order = 2)] [stringlength(50)] public string name { get; set; } } public partial class item4 { [key] [column(order = 0)] [stringlength(50)] public string item1name { get; set; } [key] [column(order = 1)] [stringlength(50)] public string item2name{ get; set; } [key] [column(order = 2)] [stringlength(50)] public string item3name{ get; set; } [key] [column(order = 3)] [stringlength(50)] public string name{ get; set; } }
(i omitted navigation properties, there.)
with relationships defined this:
modelbuilder.entity<item1>() .hasmany(e => e.item2s) .withrequired(e => e.item1) .hasforeignkey(e => new { e.item1name}) .willcascadeondelete(false); modelbuilder.entity<item2>() .hasmany(e => e.item3s) .withrequired(e => e.item2) .hasforeignkey(e => new { e.item1name, e.item2name }) .willcascadeondelete(false); modelbuilder.entity<item3>() .hasmany(e => e.item4s) .withrequired(e => e.item3) .hasforeignkey(e => new { e.item1name, e.item2name, e.name}) .willcascadeondelete(false);
now when try insert item3 existingitem2.item3s this:
item3 newitem3 = new item3(); newitem3.item4s.add(new item4()); existingitem2.item3s.add(newitem3); db.savechanges();
i following error:
cannot insert value null column 'item1name', table 'database.dbo.item4s'; column not allow nulls. insert fails.\r\nthe statement has been terminated.
the weird thing is, after insert 1 item3 existingitem2.item3s without nested item4, can insert nested items using above procedure without errors. i've tried explicitly designating keys [databasegenerated(databasegeneratedoption.none)]
, makes no difference. @ play here?
edit
i narrowed down bug , why happens , not. in program, in addition described, setting existingitem2.someunrelatednonkeyintegervalue = somevalue;
. turns out, if change value (it got set always) ef generate failing query. seems bug in ef, right? worked around this:
if (existingitem2.someunrelatednonkeyintegervalue != somevalue) { existingitem2.someunrelatednonkeyintegervalue = somevalue; db.savechanges(); } item3 newitem3 = new item3(); newitem3.item4s.add(new item4()); existingitem2.item3s.add(newitem3); db.savechanges();
(by way, integer doesn't contain id in name, , isn't marked key in database.)
without knowing database table...
if column maps item1name of same datatype property in model, can rememdy altering table column allow nulls or initialising models properties within constructor.
public partial class item3 { public item3() { item1name = string.empty; item2name = string.empty; name = string.empty; } [key] [column(order = 0)] [stringlength(50)] public string item1name { get; set; } [key] [column(order = 1)] [stringlength(50)] public string item2name{ get; set; } [key] [column(order = 2)] [stringlength(50)] public string name { get; set; } }
Comments
Post a Comment