Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Entity Framework: Primary key violation

Entity Framework: Primary key violation

Scheduled Pinned Locked Moved C#
databasehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    I try to add an object to a database, but I get an exception Code:

        DataModelContainer cont = new DataModelContainer();
    

    (...)

            cont.AddToStudentSet(cont.StudentSet.CreateObject());
            cont.SaveChanges();
    

    After running the code twice, I get: Exception: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. The Id of Student has an AutoIncrement set to TRUE in the database. On the first run a single Student record is created with Id 0. Later, no data is saved to the table at all. The reason of why is it happening seems to be obvious, but it should work anyway. Any help appreciated

    Greetings - Jacek

    D 1 Reply Last reply
    0
    • L Lutoslaw

      I try to add an object to a database, but I get an exception Code:

          DataModelContainer cont = new DataModelContainer();
      

      (...)

              cont.AddToStudentSet(cont.StudentSet.CreateObject());
              cont.SaveChanges();
      

      After running the code twice, I get: Exception: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. The Id of Student has an AutoIncrement set to TRUE in the database. On the first run a single Student record is created with Id 0. Later, no data is saved to the table at all. The reason of why is it happening seems to be obvious, but it should work anyway. Any help appreciated

      Greetings - Jacek

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Well, since you already have a student record with an Id of 0, you can't add another one. ID's should not start with 0.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      L 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Well, since you already have a student record with an Id of 0, you can't add another one. ID's should not start with 0.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        The scenario was: 1. I had designed tables with an inheritance relationship. 2. Decided that it was not a good idea 3. Removed an inheritance relationship 4. Added required IDs to the tables and set IsIdentity to true. 5. Forgot to set an identity property to "Identity". Well...

        Greetings - Jacek

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups