i`am using ef7 rc2 in project. there 2 related classes
public sealed class member { ...... public memberstate state { get; set; } } public sealed class memberstate { public guid id { get; set; } public org org { get; set; } .... }
and ef configuration:
modelbuilder.entity<memberstate>(entity => { entity.totable("state"); entity.haskey(e => e.id); entity.property<guid>("idf_org"); entity.hasone(e => e.org).withmany().hasforeignkey("idf_org"); ..... }); modelbuilder.entity<member>(entity => { entity.haskey(e => e.id); entity.property<guid>("idf_state"); entity.hasone(member => member.state).withmany().hasforeignkey("idf_state"); });
so, in time whant set null org field in membersstate.
mwmbwe.state = new memberstate() { id = guid.newguid(), .... org = null }; _provider.savechanges();
an exception occurs when saving changes database. ef trying insert related org item null pk. is way awoid ?
also tryed solve issue direct executesqlcommand, , fetching new memberstate ef provider, raises exception too: "object reference not set instance of object."
in database field, containing idf_org marked 'allow null'
if association between memberstate , org optional need specify in fluent api:
entity.hasone(e => e.org) .withmany() .hasforeignkey("idf_org") .isrequired(false);
as result ef core let save memberstate without org @ runtime.