Entity Framework 4 getting primary key ID for inserted record

后端 未结 3 1202
后悔当初
后悔当初 2020-12-15 03:11

I am using the Entity Framework for inserting a row into my sql database. If I was to be using a stored procedure then I would be able to return the primary key for the reco

3条回答
  •  無奈伤痛
    2020-12-15 03:37

    Yes of course you can do this. See example:

    int id = 0;
    
    using (PC2Entities objectContext = new PC2Entities())
    {
       objectContext.ClientContacts.AddObject(clientContact);
       objectContext.SaveChanges();
       id = clientContact.Id;
    
       transaction.Complete();
    }
    

    id is the PK.

提交回复
热议问题