views:

50

answers:

2

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 :(

+2  A: 

Change the store procedure on the database to handle Insert of duplicates.

Vivek
I don't have stored procedures backing this data. It just uses the Linq-to-SQL to do the data inserts.
Vaccano
You can attach a store procedure to do your inserts/updates in Linq-To-SQL. You could write a single procedure to do both insert and update. This might be more effecient way to handle duplicates rather than in C#.
Vivek
Also note if you do this through a stored procedure you can use the new MERGE syntax to perform the insert and update in one go. I think that was added in MS SQL 2008
AaronLS
Hmmmm, I will look into this
Vaccano
+1  A: 

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

AaronLS