Linq2SQL: Update object not created in datacontext

后端 未结 3 1743
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:01

Normally when you update an object in linq2sql you get the object from a datacontext and use the same datacontext to save the object, right?

What\'s the best way to

3条回答
  •  我在风中等你
    2021-01-13 15:10

    I think you have 2 options here:

    1) Attach the object to the DataContext on which you will do your save 2) Using the primary key on your object, grab an instance that is attached to your context (e.g. do a FirstOrDefault()), and then copy the data over from the modified object to the object that has a context (reflection might be useful here).

    Rick Strahl has a very good blog article on attaching entities to a context at http://www.west-wind.com/weblog/posts/134095.aspx, particularly in regards to some of the problems you might encounter.

提交回复
热议问题