c# - EF6 - error when insert entity with related entites only by navigation property -
i need insert entity related entity inside, both in single dbset.add invocation.
one-to-many between course , courseprofesor (courseprofesor entity connecting courses , profesors)
entities:
public class course { public course() { } [key, databasegenerated(databasegeneratedoption.identity)] public int id { get; set; } ... public virtual icollection<courseprofesor> profesors { get; set; } } public class courseprofesor { public courseprofesor() { } [key] public int id { get; set; } [required, index, column(order = 0)] public int courseid { get; set; } [required, index, column(order = 1)] public int profesorid { get; set; } ... [foreignkey("courseid")] public virtual course course { get; set; } [foreignkey("profesorid")] public virtual profesor profesor { get; set; } }
mappings:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<course>().hasmany(x => x.profesors).withrequired(x => x.course); modelbuilder.entity<courseprofesor>().hasrequired(x => x.course).withmany(x => x.profesors); modelbuilder.entity<courseprofesor>().hasrequired(x => x.profesor).withmany(x => x.courses); }
controller:
public actionresult add(course course, int profesorid = 0) { if (profesor > 0) { course.profesors = new list<courseprofesor>(); course.profesors.add(new courseprofesor() { course = course, courseid = 0, profesorid = profesorid, = datetime.now, role = ... }); } facade.create(course); return json(new {statustext = "course added"}); }
facade.create(entity) executes createcommand in turn invoke
dbcontext.set(entity.gettype()).add(entity)
the exception get:
the changes database committed successfully, error occurred while updating object context. objectcontext might in inconsistent state. inner exception message: referential integrity constraint violation occurred: property value(s) of 'course.id' on 1 end of relationship not match property value(s) of 'courseprofesor.courseid' on other end
how assign courseprofesor.courseid if don't know id of course yet, since both new entities?
can see in controller code, used worked out setting navigation property , ef auto-populate foreign key accordingly.
important: working fine on ef5, i got error after updating ef6
any clues why ef6 throws exception while ef5 didn't? , how solve without having first create course , courseprofesor relationship entity?
a couple of things stand-out:
course.profesors.add(new courseprofesor() { course = course, courseid = 0, profesorid = profesorid, = datetime.now, role = ... });
when using navigation properties steer away defining fks fields in entities, if need define them, should avoid setting them. use navigation properties only. setting fks can misleading because if pass course out it's courseprofessors , consume it, there professorid set, no professor reference available.
regarding specific problem probable issues courseid = 0 set above, , two-way mapping between course , courseprofessor.
modelbuilder.entity<course>().hasmany(x => x.profesors).withrequired(x => x.course); //modelbuilder.entity<courseprofesor>().hasrequired(x => x.course).withmany(x => x.profesors); modelbuilder.entity<courseprofesor>().hasrequired(x => x.profesor).withmany(x => x.courses);
try removing redundant mapping.
also definition of linking table has id pk, isn't set generation option. 2 fks set order=0 , order=1 well, looks odd without considering pk?
beyond that, can't facade pattern. eliminate possibility, happens if go db context courses dbset?
context.courses.add(course); context.savechanges();
Comments
Post a Comment