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 ins开发者_StackOverflow中文版ert or update as needed? I have quite a few object types, so that will get tedious really fast :(
Change the store procedure on the database to handle Insert of duplicates.
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
精彩评论