Sami
Sami

Reputation: 304

Get ID of an Object After adding it to Database using Entity Framework

I am beginner to Entity Framework.

Let's say I have a Person Entity and I need to add a new Person to Database.

My Database has a Person Table with Id, Name, Age, and email. where id is the PK.

To add a new Person. My code will look:

DatabaseEntities context = new DatabaseEntities();
Person p = new Person("name", age, "email");
context.AddToPersons(p);
context.SaveChanges();

My Question is: After adding the new person, How can I get the id of that person.

Upvotes: 0

Views: 515

Answers (1)

webtrifusion
webtrifusion

Reputation: 4556

The id should automatically be set by the context.

Upvotes: 4

Related Questions