entity framework - EF code first add model related exist childs -
i try add new model db model have child exists , got error:
"a referential integrity constraint violation occurred: property value(s) of 'category.id' on 1 end of relationship not match property value(s) of 'lesson.categoryid' on other end."
for example:
public class category { public int id { get; set; } public string name{ get; set; } public list<lesson> lessons{ get; set; } } public class lesson { public int id { get; set; } public string name{ get; set; } public list<category> categories { get; set; } }
i think category id still 0 until db generate new id , lesson.categoryid filled in 0 not exist in categories
save code:
db.categories.add(obj); db.changetracker.entries() .where(x => x.entity basemodel && x.state == entitystate.added && ((basemodel)x.entity).id > 0) .tolist().foreach(x => x.state = entitystate.unchanged);
any idea how fix that? anyway
Comments
Post a Comment