c# - with - how to get inserted record id in entity framework

How can I get Id of inserted entity in Entity framework? (8)

All answers are very well suited for their own scenarios, what i did different is that i assigned the int PK directly from object (TEntity) that Add() returned to an int variable like this;

using (Entities entities = new Entities())
      int employeeId = entities.Employee.Add(new Employee
                            EmployeeName = employeeComplexModel.EmployeeName,
                            EmployeeCreatedDate = DateTime.Now,
                            EmployeeUpdatedDate = DateTime.Now,
                            EmployeeStatus = true

      //...use id for other work

so instead of creating an entire new object, you just take what you want :)

EDIT For Mr. @GertArnold :

I have a problem with Entity Framework in Asp.net. I want to get the Id value whenever I add an object to database. How can I do this?

I come across a situation where i need to insert the data in the database & simultaneously require the primary id using entity framework. Solution :

long id;
IGenericQueryRepository<myentityclass, Entityname> InfoBase = null;
    InfoBase = new GenericQueryRepository<myentityclass, Entityname>();
    id = entityclassobj.ID;
    return id;

It is pretty easy. If you are using DB generated Ids (like IDENTITY in MS SQL) you just need to add entity to ObjectSet and SaveChanges on related ObjectContext. Id will be automatically filled for you:

using (var context = new MyContext())

  int id = myNewObject.Id; // Yes it's here

Entity framework by default follows each INSERT with SELECT SCOPE_IDENTITY() when auto-generated Ids are used.

There are two strategies:

  1. Use Database-generated ID (int or GUID)


    You should perform SaveChanges() to get the ID for just saved entities.


    Can use int identity.

  2. Use client generated ID - GUID only.

    Pros: Minification of SaveChanges operations. Able to insert a big graph of new objects per one operation.


    Allowed only for GUID

When you use EF 6.x code first

    public Guid Id { get; set; }

and initialize a database table, it will put a


inside the table properties under the header Default Value or Binding, allowing the ID to be populated as it is inserted.

The problem is if you create a table and add the


part later, future update-databases won't add back the (newsequentialid())

To fix the proper way is to wipe migration, delete database and re-migrate... or you can just add (newsequentialid()) into the table designer.

You need to reload the entity after savechanges. Because it has been altered by a database trigger which cannot be tracked by EF. SO we need to reload the entity again from the DB,



int id = myNewObject.Id;

Look at @jayantha answer in below question:

How can I get Id of inserted entity in Entity framework when using defaultValue?

Looking @christian answer in below question may help too:

Entity Framework Refresh context?

Repository.addorupdate(entity, entity.id);
Var id = entity.id;

This will work.