Brendan Vogt
Brendan Vogt

Reputation: 26028

How to get the auto incremented value in entity framework code first

I am using Entity Framework 4.1 code first. I have a product table that has an Id column that is an auto incremented column. When I add the the Product instance, how do I get this new id or the updated Product (returned with properties)?

My repository code:

MyContext db = new MyContext();

public void Insert(Product product)
{
     db.Products.Add(product);
     db.SaveChanges();
}

Upvotes: 4

Views: 4498

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

After performing the insert look at the product.Id property (or whatever your id property is called). It will be updated by EF with the value assigned by the database.

Upvotes: 7

Related Questions