Entity framework DBcontext add child row -
as understand, add child row, can either do
parent.children.add(child);
or
child.parents= parent
then can mycontext.savechanges();
somehow second solution doesn't insert work @ all. here's code:
countmaster countmaster = (from b in pmdatacontext.countmasters b.reportdate == reportdate && b.department == dept select b).singleordefault(); countdetail countdetail = new countdetail { categoryid = 1, statusid = 1 }; //countmaster.countdetails.add(countdetail); countdetail.countmaster = countmaster; pmdatacontext.savechanges();
when adding entity collection navigation property or setting reference navigation property of tracked entity ef add entity context in entitystate.added state. due objectstatemanager detecting changes made tracked entity.
if working other way around, is, starting untracked entity , setting/adding tracked entities reference/collection properties, objectstatemanager has no way of detecting changes make untracked entity. therefore need explicitly add entity context before savechanges().
countdetail.countmaster = countmaster; context.countdetails.add(countdetail); pmdatacontext.savechanges();
Comments
Post a Comment