如何在实体框架中获取最近添加的对象的主键?

时间:2021-12-05 22:51:03

I don't know if there's something like this

我不知道是否有这样的事情

`//context.Add(...) stuff...
var primaryKeyOfRecentObjectAddedToDB = context.SaveChanges();`

I just want the primary key the database gave to my most recently added object.

我只想要数据库给我最近添加的对象的主键。

Thanks for the help!

谢谢您的帮助!

3 个解决方案

#1


1  

You have access to it on your object, as soon as it is saved:

一旦保存,您就可以在对象*问它:

var foo = new Foo { Id = Guid.NewGuid(); }

context.Add(foo);
context.SaveChanges();
var id = foo.Id;

Better yet, use a Guid (instead of an identity column) for your id and you have access to it before you hit your database.

更好的是,为您的ID使用Guid(而不是标识列),并且在访问数据库之前可以访问它。

var foo = new Foo { Id = Guid.NewGuid(); }

var id = foo.Id; // have access to the id here

context.Add(foo);
context.SaveChanges();

#2


1  

If you have multiple entities of type MyEntity in the context which have to be inserted into the DB you could use something like this (in EF 4.1):

如果在上下文中有多个MyEntity类型的实体必须插入到DB中,您可以使用类似的东西(在EF 4.1中):

// save references to the entities which will be added in a local list
var listOfEntitiesToAdd = context.ChangeTracker.Entries()
    .Where(e => e.State == EntityState.Added)
    .Select(e => e.Entity)
    .OfType<MyEntity>()
    .ToList();

// insert the entities, state will change to Unchanged,
// PK property (Id) gets populated
context.SaveChanges();

if (listOfEntitiesToAdd.Count > 0)
{
    // get highest created Id
    int maxId = listOfEntitiesToAdd.Max(e => e.Id);
}

#3


0  

Assuming your primary key is a property of your entity and assigned by the DB it will be populated after you execute SaveChanges():

假设您的主键是您的实体的属性并由DB分配,它将在您执行SaveChanges()后填充:

context.Add(someFoo);
context.SaveChanges();
long myId = someFoo.Id;

#1


1  

You have access to it on your object, as soon as it is saved:

一旦保存,您就可以在对象*问它:

var foo = new Foo { Id = Guid.NewGuid(); }

context.Add(foo);
context.SaveChanges();
var id = foo.Id;

Better yet, use a Guid (instead of an identity column) for your id and you have access to it before you hit your database.

更好的是,为您的ID使用Guid(而不是标识列),并且在访问数据库之前可以访问它。

var foo = new Foo { Id = Guid.NewGuid(); }

var id = foo.Id; // have access to the id here

context.Add(foo);
context.SaveChanges();

#2


1  

If you have multiple entities of type MyEntity in the context which have to be inserted into the DB you could use something like this (in EF 4.1):

如果在上下文中有多个MyEntity类型的实体必须插入到DB中,您可以使用类似的东西(在EF 4.1中):

// save references to the entities which will be added in a local list
var listOfEntitiesToAdd = context.ChangeTracker.Entries()
    .Where(e => e.State == EntityState.Added)
    .Select(e => e.Entity)
    .OfType<MyEntity>()
    .ToList();

// insert the entities, state will change to Unchanged,
// PK property (Id) gets populated
context.SaveChanges();

if (listOfEntitiesToAdd.Count > 0)
{
    // get highest created Id
    int maxId = listOfEntitiesToAdd.Max(e => e.Id);
}

#3


0  

Assuming your primary key is a property of your entity and assigned by the DB it will be populated after you execute SaveChanges():

假设您的主键是您的实体的属性并由DB分配,它将在您执行SaveChanges()后填充:

context.Add(someFoo);
context.SaveChanges();
long myId = someFoo.Id;