Linq2SQL: Update object not created in datacontext

后端 未结 3 1705
孤独总比滥情好
孤独总比滥情好 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:26

    To update an existing but disconnected object, you need to "attach" it do the data context. This will re-use the existing primary key etc. You can control how to handle changes- i.e. treat as dirty, or treat as clean and track future changes, etc.

    The Attach method is on the table - i.e.

    ctx.Customers.Attach(customer); // optional bool to treat as modified
    

提交回复
热议问题