开发者

insert in one DataContext, submit in another

开发者 https://www.devze.com 2023-02-18 23:44 出处:网络
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 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号