Thursday, August 7, 2008

LINQ to SQL!! Key Conflicts

Ok all - just a cautionary tale... if you see anything similar to the following when working with LINQ to SQL, consider what I'm about to tell you...

"Cannot add an entity with a key that is already in use."
or any other message about conflicts or whatever.....

I've had this problem probably four times, and for some reason, the symptoms still fail to trigger my previous memories.....and btw, there seems to be no tooling around how to figure out what data LINQ is talking about when it makes claims about bad primary keys,etc...(if I'm wrong, please let me know..)

Anyway - I use SQL Server generated identities for rows... and if that is not annotated on the LINQ to SQL generated code, it will not use the generated key - it will assume the key you are giving it is accurate (be it a 0 or null or whatever....)

So just remember this...... it's annoying....and it's probably, overall now, cost me a week of dev time for this project.... BOOOO


Till Next Time...

No comments: