开发者

Linq to SQL with unique index using ignore duplicate keys option and an Identity column

开发者 https://www.devze.com 2023-02-09 13:37 出处:网络
_db.Comme开发者_开发技巧nts.InsertAllOnSubmit(comments); _db.SubmitChanges(); There is a unique index on this table with IGNORE_DUP_KEY = ON, i know that i\'m inserting duplicates i just want to ign
_db.Comme开发者_开发技巧nts.InsertAllOnSubmit(comments);
_db.SubmitChanges();

There is a unique index on this table with IGNORE_DUP_KEY = ON, i know that i'm inserting duplicates i just want to ignore them, i'd rather not check each record manually, as this will be slower.

It throws the following:

InvalidOperationException: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

I'm assuming this is because the value returned to L2S for the primary key field (it's an IDENTITY Column) is null. To me this seems almost like a bug.

Anybody got any ideas, other than individual insert with try/catch or manual checking?


You can change the property to be nullable. This seems more like a work around than a solution. You can change it in the generatedc code (bad idea) or create a subclass and the new keyword.

int? ID;

0

精彩评论

暂无评论...
验证码 换一张
取 消