How do I retrieve the Primary Key when inserting a record using Linq to Entities? RRS feed

  • Question

  • Using Linq to Entities, how do I retrieve the Primary key when I insert a new record?
    Saturday, July 6, 2013 6:54 AM


  • Hi bifrosr.pro;

    When you insert a new record into a table the insert will be sent to the database and right after that happens the database returns the primary key back to your application and EF will update the record in the local copy of that record. Therefore to find out what primary key was assigned to the record all you need to do is read the primary key field from the local copy.


    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.

    • Marked as answer by Bifrost Ltd Saturday, July 6, 2013 3:08 PM
    Saturday, July 6, 2013 12:29 PM