Page 1 of 1

DataContext life cicle

Posted: Wed 09 Aug 2017 14:34
by -marcelo-
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

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
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

Re: DataContext life cicle

Posted: Thu 10 Aug 2017 15:10
by Shalex
Thank you for confirming solution. As we understood, no investigation on our side is required.