How To Insert A Record Using The Entity Framework And Get Back The Identity Of the Record



TODO:

Have you ever wanted to use the Entity Framework to insert a new record into the database, and get back the identity associated with it?

 

SOLUTION:

using(DataEntities dataEntities = new DataEntities())
{
       FormNumber formNumber = new FormNumber();
       dataEntities.AddToFormNumbers(formNumber);
       dataEntities.SaveChanges();
       return formNumber.Id;
}

 

NOTES:

This assumes you have an EDMX created called "DataEntities".  It assumes you have a table called "FormNumber", with an Identity column called "Id".



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading