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

10,241

Solution 1

Have a look at this forum

Solution 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.

Share:
10,241

Related videos on Youtube

Yves
Author by

Yves

Updated on June 04, 2022

Comments

  • Yves
    Yves almost 2 years

    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.

  • karpanai
    karpanai over 9 years
    Perfect Solution. Very important update DBML which mostly forget :-)
  • Mike Gledhill
    Mike Gledhill about 7 years
    Update the .dbml file: Yup, this solved it for me aswell... damn !!