CountCet
CountCet

Reputation: 4573

LINQ to SQL: Insert non-identity Primary Key

I am using LINQ to SQL for the first time on a small project. My tables have Primary Keys that are not identity columns because I am essentially importing from another database. The relationship is a many-to-many with a linking table. Is there a way to tell LINQ to ignore the insert if it is a duplicate?

I have already tried to check the current tables if the object exists and if so, skip to the next but because of the relationships, it makes it difficult.

I can give more details if necessary.

Upvotes: 1

Views: 1683

Answers (1)

Chad Moran
Chad Moran

Reputation: 12854

Instead of querying for the whole object what you can do is an Any query...

DB.Items.Any(i => i.ID == myObj.ID);

This will do an optimized query for checking if it exists first.

As far as having LINQ to SQL handle this for you, I'm pretty sure it cannot.

Upvotes: 2

Related Questions