Assign my own value to Identity RRS feed

  • General discussion

  • Hey,

    Let's say we have a table named News. Most of the time the primary key of news will be auto_generated (StoreGeneratedPattern = Identity right?) but from time to time we need to be able to insert News with primary key of our choice. Is that possible to implement with EF?

    Monday, August 26, 2013 5:51 PM

All replies

  • I will explain a bit more...

    We have two databases one in the Cloud and one is Local, the model is the same. First we have to insert our News into the Cloud (here we use auto_increment), then we insert the same news into local database with the same ID that was assigned on the cloud. If I change the StoreGeneratedProcedure to none, primary key doesn't update when I run SaveChanges();

    Monday, August 26, 2013 5:55 PM
  • Hello,

    I'm afraid that Entity Framework can't realize what you want.

    But you can use call a procedure to operate the insert.

    Tuesday, August 27, 2013 5:10 AM
  • I don't think you can do it. For sure, you can remove the PK and identity off of the and add records and assign your own incremental key and enable the PK as identity. it will work as long as you don't assign numbers that are already in usage. But doing something through EF is probably not going to work.

    Tuesday, August 27, 2013 9:45 AM