ON INSERT: Cannot add an entity with a key that is

2019-07-20 06:51发布

问题:

I have no code behind and the above error keep prompting on INSERT. The DBML is refresh!

Exception Details: System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.

回答1:

Have a look at this forum



回答2:

Sounds to me like Identity Specification is not enabled on the table's index column. Enable it and update the DBML.

Of course, if your primary key is something that doesn't support Identity Specification, you will need to generate your own primary keys. Entity Framework doesn't use default values you may have set for a column, so if you use a uniqueidentifier column and have set the default value to NEWID(), that might be your problem. In that case, use

myObject.Id = Guid.NewGuid();

from your code.