Get Record ID in Entity Framework after insert
Asked Answered
K

4

22

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.

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 ?

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

Knickknack answered 6/6, 2013 at 6:6 Comment(3)
Possible duplicate of How can I get Id of inserted entity in Entity framework?Caracaraballo
@Michael Freidgeim it's not a duplicate because Bishan wants it with EntityDataSource and the question and answers there do not relate to EntityDataSource.Homogenesis
Check this: codingfusion.com/Post/…Cryohydrate
K
4

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.

OnInserted="clientDataSource_OnInserted"
Knickknack answered 6/6, 2013 at 6:41 Comment(0)
B
44

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

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:

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;
Brasserie answered 6/6, 2013 at 6:16 Comment(1)
I'm using DetailsView with EntityDataSource and binding EntityDataSource directly with Entity Model. so i'm not creating objects to insert data.Knickknack
M
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.

Like MyObject.Id will give you the new Id

Mukden answered 6/6, 2013 at 6:10 Comment(1)
I'm using DetailsView with EntityDataSource and binding EntityDataSource directly with Entity Model. so i'm not creating objects to insert data.Knickknack
K
4

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.

OnInserted="clientDataSource_OnInserted"
Knickknack answered 6/6, 2013 at 6:41 Comment(0)
D
0

Thanks, I spent 3 days searching and searching with complicated results but this solution is brilliant! Here it is in 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
Doolie answered 14/11, 2013 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.