c# - Duplicate record in database when call SaveChanges() -
hi have problem ef. in application have load database content populate datagrid.
usercontrol :
contenus = new list<contenu>(); contenus = sacoche.contenus.tolist(); // sacoche in parameter of contructor contenuviewsource.source = contenus; contenuview = (listcollectionview)contenuviewsource.view; contenuview.refresh(); everything work fine, when try add others contenus duplicate record in database. difference between duplicated record first record loose foreign key.
here add contenuto sacoche:
editablesacoche = sacochedal.dbcontext.sacoches.include("contenus").first(i => i.sacocheid == editablesacoche.sacocheid); editablesacoche.contenus = contenus; sacochedal.dbcontext.savechanges(); all sacoche , add contenu , call savechanges().
here result : 
edit: tried new items failed.
list<contenu> contenuajoute = contenus.except(editablesacoche.contenus.tolist()).tolist(); in contenuajoutei records if equal ...
try this:
editablesacoche = sacochedal.dbcontext.sacoches.include("contenus").first(i => i.sacocheid == editablesacoche.sacocheid); editablesacoche.contenus = null; editablesacoche.contenusid = contenus.id; sacochedal.dbcontext.savechanges();
Comments
Post a Comment