DataContext life cicle
Posted: Wed 09 Aug 2017 14:34
LinqConnect and Entity Developer newbie.
My question is related to the DataContext life cicle.
If I want to insert a new entity, I could do
EDIT
I was seeing the Attach method overloads, specifically Attach(changed, original).
Having that, I could use an instance of DataContext to read the "original" entity and dispose it immediately. After the user is done with editing on a copy (changed) a could instance a new DataContext, Attach both instances and call SubmitChanges()?
EDIT2
Tested: it´s the way to go. Attach( changed, original ); then SubmitChanges();
TIA
Marcelo
My question is related to the DataContext life cicle.
If I want to insert a new entity, I could do
Code: Select all
SomeEntity instance = new SomeEntity();
// set the properties
using (DataContext context = new DataContext()) {
context.SomeEntities.InsertOnSubmit(instance);
context.SubmitChanges();
}
// the life cicle of the DataContext is very short
I was seeing the Attach method overloads, specifically Attach(changed, original).
Having that, I could use an instance of DataContext to read the "original" entity and dispose it immediately. After the user is done with editing on a copy (changed) a could instance a new DataContext, Attach both instances and call SubmitChanges()?
EDIT2
Tested: it´s the way to go. Attach( changed, original ); then SubmitChanges();
TIA
Marcelo