none
Does EF 6 Allow for Primary Key Updates RRS feed

  • Question

  • In past versions of EF, developers could not provide update functionality to a primary key, which is found in some database designs that employ simple 2 or 3 character codes (from the domain) instead of abstract surrogate keys.  With the newly released EF 6, is this limitation eliminated? (assume workflow Existing Db with Model first) 

    If not, other than altering the database with numerous new surrogate keys, what other options does Microsoft provide?

    Sunday, February 23, 2014 2:14 AM

Answers

  • EF still doesn't support PK updates through change tracking and SaveChanges()

    If you want to update a PK in an EF application, you can just use a SQL query through DbContext.Database.ExecuteSqlCommand().

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by Jim Lutterbach Thursday, February 27, 2014 4:23 AM
    Wednesday, February 26, 2014 8:19 PM

All replies

  • Hi Jim;

    From my understanding that is still true, the primary key cannot be modified. There is no work around using Entity Framework.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, February 23, 2014 3:26 AM
  • Thank you Fernando,

    I need to follow-up with a few more questions, however, since the implementation of surrogate keys in place of the current natural keys would not be a trivial process. 

    To elaborate on the issue just a bit more, the tables in question are lookup-tables (as opposed to transaction or fact tables) that provide two letter classifications as a Primary Key.  With ON UPDATE CASCADE established to the transactions tables, a rare, but allowable modification is possible to the two letter codes.  Within the database (and with legacy non-EF clients) this does not present a problem, since the aforementioned referential integrity governs and foreign keys would be appropriately updated in the database.  However, EF does not appear to be able to model this database behavior. 

    Since at its heart EF is generating Db access code, for the rare but possible PK UPDATE, would a logical alternative be to implement our own access code just for UPDATES, while still using EF for CREATION, RETRIVAL and DELETES on the rest of the table(s)?

    Could Microsoft comment on the validity of this approach?
    Any additional references and guidance would be greatly appreciated.  Thank you.

    Wednesday, February 26, 2014 4:30 AM
  • Hi Jim;

    Seeming I am not a Microsoft employee and do not know the inner workings of that part of the EF Framework I would not be able to comment on your last question. Someone like Fred Bao who monitors this forum and works for Microsoft can best answer that question or has access to the people who can..


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, February 26, 2014 7:05 PM
  • EF still doesn't support PK updates through change tracking and SaveChanges()

    If you want to update a PK in an EF application, you can just use a SQL query through DbContext.Database.ExecuteSqlCommand().

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by Jim Lutterbach Thursday, February 27, 2014 4:23 AM
    Wednesday, February 26, 2014 8:19 PM
  • Thanks David.  This should take care of our issue.

    Jim Lutterbach
    Unified Informatics LLC
    Indianapolis IN

    Thursday, February 27, 2014 4:23 AM