none
Issue while creating Primary Key on saving record using Entity Framework RRS feed

  • Question

  •  Hi 

    I have an application in which Entity Framework is used. so When I am trying to insert a record in to a certain table it gives me error on context.Save function Error is of duplicate Primary Key, because PK that is generated on Save already exist in that table.

    Can any body help me understanding issue, whey PK generated is behind actual value in DB, if DB has 1000 rows my app is generated new PK less then 1000 so it is violating PK constraint.

    • Moved by CoolDadTx Tuesday, September 15, 2015 1:56 PM EF related
    Tuesday, September 15, 2015 11:39 AM

All replies

  • You must be generating these keys yourself, and there are already records in the table with the keys. That's the only reason this can be happening. Normally, one let's the DB create the keys, because the table's primary key is auto generated by using an Identity column as the primary-key. If you yo are doing that, then there should be no problem.  You can get help at the forum below.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Tuesday, September 15, 2015 1:26 PM
  • I am not generating key's my self and DB is generating key's but it is lagging behind some how.
    Tuesday, September 15, 2015 4:03 PM
  • I am not generating key's my self and DB is generating key's but it is lagging behind some how.

    It might be that somehow you don't have the Identity column as the primary-key in the table set to increment by 1 or something like that. You may have it set, but it might not be incrementing.

    Tuesday, September 15, 2015 8:44 PM
  • So give some information on the issue is that there are two application's which are doing same thing one is automated service and other is Entry form to do same thing manual both app's uses Entity Framework

    Automated application is working fine and able to insert value and gets new key properly.

    But Entry Form application gets Key which is already there in Table and some time it lag's behind by more then 200 key's.

    So i think there is no issue on DB side all PK's are set properly, My guess is issue with EF resource files.

    Because we refreshed DB with data from third party on  Monday but  .msdl ,ssdl & csdl are from last week deployed with our Entry Form application.

    Wednesday, September 16, 2015 12:18 PM
  • But Entry Form application gets Key which is already there in Table and some time it lag's behind by more then 200 key's

    Are you using Oracle or what?

    Wednesday, September 16, 2015 1:39 PM
  • using SQL
    Wednesday, September 16, 2015 3:13 PM
  • My guess is issue with EF resource files.

    I don't know how you reached this situation, but maybe you need to start over or re-do it.

    Wednesday, September 16, 2015 5:41 PM