insert in one DataContext, submit in another
I have two System.Data.Linq.DataContext
instances of same type. There is a table Table1
in DataContext
. I add an object in first DataContext
instance to the Table1
using the InsertOnSubmit
method and I call SubmitChanges
in se开发者_如何转开发cond instance.
Will the object be inserted?
No. The SubmitChanges call only looks at the changes logged in the current context. It will not know about changes in a different data context and won't submit them. There are some things you can do that would make it appear so (like adding the object to a collection in on an object in the submitted context), but if they are truly independent, then it won't work.
Short answer: no.
My recommendation is to follow the repository pattern, which declares a single instance of the data context at the class level. Scott Gu's NerdDinner, although it applies specifically to ASP.NET MVC, still has some good insight on how to build a data model using this pattern: http://nerddinnerbook.s3.amazonaws.com/Part3.htm
精彩评论