插入后在实体框架中获取记录ID

时间:2022-08-30 09:35:01

I'm developing an ASP.net application using Entity Framework. I'm using DetailsView to insert data into database. There is a table as Client and its primary key is client_id. client_id is auto generated by database. I need to get auto generated client_id after inserting a record into Client table and assign it to a hidden field for future use.

我正在使用Entity Framework开发ASP.net应用程序。我正在使用DetailsView将数据插入数据库。有一个表作为客户端,其主键是client_id。 client_id由数据库自动生成。在将记录插入Client表后,我需要获取自动生成的client_id,并将其分配给隐藏字段以供将来使用。

I searched about this and I found lot of solutions. But I don't know how to use them since I'm new to asp.net. I found that Entity Framework automatically populates business objects with the db-generated values after call SaveChanges(). My question is where should I call this in my partial class ? What is the event ?

我搜索了这个,我找到了很多解决方案。但我不知道如何使用它们,因为我是asp.net的新手。我发现在调用SaveChanges()之后,Entity Framework会自动使用db生成的值填充业务对象。我的问题是我应该在我的部分课程中将其称为何处?活动是什么?

I'm using DetailsView with EntityDataSource and binding EntityDataSource directly with Entity Model, so I'm not creating objects to insert data.

我正在使用DetailsView和EntityDataSource并将EntityDataSource直接与实体模型绑定,所以我不是要创建插入数据的对象。

4 个解决方案

#1


39  

Following the call to _dbContext.SaveChanges(), the entity will automatically be updated with its new identity field value.

在调用_dbContext.SaveChanges()之后,实体将自动使用其新的标识字段值进行更新。

The following code assumes your Entity Framework entity container name is MyEntities, and your database's Client table has at least the two following fields:

以下代码假定您的Entity Framework实体容器名称是MyEntities,而您的数据库的Client表至少包含以下两个字段:

client_id   int identity
client_name varchar(25)

Your code might look something like this:

您的代码可能如下所示:

// Establish DbContext
private readonly MyEntities _dbContext = new MyEntities();

// Create new client table entity and initialize its properties
var clientEntity = new Client { client_name="MyCo" };

// Add it to the ORM's collection of Client entities
_dbContext.Clients.Add(clientEntity);

// Save the new entity to the database
_dbContext.SaveChanges();

// Return the identity field from the existing entity,
//   which was updated when the record was saved to the database
return clientEntity.client_id;

#2


5  

After you have inserted the entity it should be updated so that the property that maps to the primary key in the database has the new PK value.

插入实体后,应更新它,以便映射到数据库中主键的属性具有新的PK值。

Like MyObject.Id will give you the new Id

像MyObject.Id会给你新的Id

#3


3  

This is what i'm looking for.

这就是我要找的。

in partial class

在部分班级

protected void clientDataSource_OnInserted(object sender, EntityDataSourceChangedEventArgs e )
{

    int newPrimaryKey = ((Client)e.Entity).ClientId;
    Debug.WriteLine(" Client ID is " + newPrimaryKey);

}

and added below line in EntityDataSource in aspx page.

并在aspx页面的EntityDataSource中添加以下行。

OnInserted="clientDataSource_OnInserted"

#4


0  

Thanks, I spent 3 days searching and searching with complicated results but this solution is brilliant! Here it is in vb.net:

谢谢,我花了3天时间搜索并搜索复杂的结果,但这个解决方案很棒!这是在vb.net中:

Protected Sub dvArticulos_ItemInserted(sender As Object, e As EntityDataSourceChangedEventArgs) Handles EntityDataSource1.Inserted
    Dim last_Serie As Long = DirectCast(e.Entity, articulos).Serie
    Session("Articulo") = last_Serie
End Sub

#1


39  

Following the call to _dbContext.SaveChanges(), the entity will automatically be updated with its new identity field value.

在调用_dbContext.SaveChanges()之后,实体将自动使用其新的标识字段值进行更新。

The following code assumes your Entity Framework entity container name is MyEntities, and your database's Client table has at least the two following fields:

以下代码假定您的Entity Framework实体容器名称是MyEntities,而您的数据库的Client表至少包含以下两个字段:

client_id   int identity
client_name varchar(25)

Your code might look something like this:

您的代码可能如下所示:

// Establish DbContext
private readonly MyEntities _dbContext = new MyEntities();

// Create new client table entity and initialize its properties
var clientEntity = new Client { client_name="MyCo" };

// Add it to the ORM's collection of Client entities
_dbContext.Clients.Add(clientEntity);

// Save the new entity to the database
_dbContext.SaveChanges();

// Return the identity field from the existing entity,
//   which was updated when the record was saved to the database
return clientEntity.client_id;

#2


5  

After you have inserted the entity it should be updated so that the property that maps to the primary key in the database has the new PK value.

插入实体后,应更新它,以便映射到数据库中主键的属性具有新的PK值。

Like MyObject.Id will give you the new Id

像MyObject.Id会给你新的Id

#3


3  

This is what i'm looking for.

这就是我要找的。

in partial class

在部分班级

protected void clientDataSource_OnInserted(object sender, EntityDataSourceChangedEventArgs e )
{

    int newPrimaryKey = ((Client)e.Entity).ClientId;
    Debug.WriteLine(" Client ID is " + newPrimaryKey);

}

and added below line in EntityDataSource in aspx page.

并在aspx页面的EntityDataSource中添加以下行。

OnInserted="clientDataSource_OnInserted"

#4


0  

Thanks, I spent 3 days searching and searching with complicated results but this solution is brilliant! Here it is in vb.net:

谢谢,我花了3天时间搜索并搜索复杂的结果,但这个解决方案很棒!这是在vb.net中:

Protected Sub dvArticulos_ItemInserted(sender As Object, e As EntityDataSourceChangedEventArgs) Handles EntityDataSource1.Inserted
    Dim last_Serie As Long = DirectCast(e.Entity, articulos).Serie
    Session("Articulo") = last_Serie
End Sub