user738383
user738383

Reputation: 607

LINQ to SQL CRUD (insert specifically) - inserting multiple items

I know you can insert new items to your SQL database (LINQ to SQL, code generated by SQLMetal.exe). You can attach new items with the Attach method in your entity table and what not, or you can edit existing records.

Now, let's say, instead of one new entity, you're presented with a lot - some of which may well already exist within the table. There is a primary key, but it's possible there may be some altered records in the collection, so the primary key probably isn't going to be the best method of figuring out what's changed.

Do I have to go through every record in my LINQ table and then compare all of its column data with all of the column data in the entities in the collection in question? This would tell me which ones are new, which ones have had changes, and which ones can be discarded. This just seems like a really long winded way of doing it.

Is there an easier way?

Thanks.

Upvotes: 1

Views: 454

Answers (1)

user1231231412
user1231231412

Reputation: 1659

I think an "UPSERT" is what your after.

It's basically a combined insert/update command for sql, if it exists update it, if not create it.

http://www.databasejournal.com/features/mssql/article.php/3739131/UPSERT-Functionality-in-SQL-Server-2008.htm

Upvotes: 0

Related Questions