简体   繁体   中英

Can Linq-to-SQL Do an Insert or update as needed?

I have a distributed app that sends data via WCF to a server to be stored in the database (SQL Server 2008).

Usually this data is new. So I just do InsertAllOnSubmit().

But occasionally, due to communication oddities with the handheld devices that run the client side I get an object that is already in the server.

Is there a way to say InsertOrUpdateAllOnSubmit? Or am I going to have to change my code to go through each object and check to see if it is in the database and then do and insert or update as needed? I have quite a few object types, so that will get tedious really fast :(

更改数据库上的存储过程以处理重复项插入。

Usually what I've seen as the pattern for the scenario when you attempt to do any kind of insert or update and there is conflict, such as objects already existing, then once you detect a conflict you request the new data and apply your updates to it. This allows you the flexibility of deciding which change wins, or prompting the user in some way letting them view the new data and decide if their data should win. It all depends on the context and business rules.

This mostly applies to updates but maybe if you think about why these conflicts are occurring for inserts you might be able to adapt your implementation to use concurrency detection and resolution techniques. http://msdn.microsoft.com/en-us/library/bb399373.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM