c# - Can we attach more than one object in entity framework and do savechanges once -


i using visual studio 2012 , entity framework 6.0 , doing crud operation have challenge face that.

i doing multiple entries in 1 form , want update or save details @ once doing this:

create object , store values , add / update attach 1 one , save changes.

but can attach multiple object of class , savechanges @ last mean after loop. running save multiple savechanges attaching multiple objects?

example:

for(int i=0; < comingrequestdata.count(); i++) {     objectexample obj = new objectexampole();      obj.value1 = comingrequestdata[i].value1;     obj.value2 = comingrequestdata[i].value2;     obj.value3 = comingrequestdata[i].value3;      context.objectexamples.attach(obj); }  context.savechanges(); 

can this? little bit confused please 1 me.

you can try shown below.

.attach

if have entity exists in database, , have constructed entity manually, use .attach.

.addobject

if have brand new object, use .addobject.

for(int i=0; < comingrequestdata.count(); i++) {     objectexample obj = new objectexampole();      obj.value1 = comingrequestdata[i].value1;     obj.value2 = comingrequestdata[i].value2;     obj.value3 = comingrequestdata[i].value3;      context.objectexamples.addobject(obj); }  context.savechanges(); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -