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 : problem duplicate

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

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -