I'm trying to determine what is the correct way to save the data to the database and return data from a column after it saves (identity index column) back to be used to save data in another table.
Should the step be to save the data in the main table 1st and then query it for the index that was created to get the value and be used to save in another column within another table?
Example, order header information is the table the 1st table that would be saved and when saved it creates a new OrderID from identity, I'll need that order number to be used in the OrderItems table that holds the order items.
Actually I want to do this via my MVC controller after running SaveChanges() method and not via a SQL statement. Just curious if there is a EF method that would get the last record inserted.
I guess I'm not understanding how the examples you showed work. I'm using EF's add then saving it.
What I'm currently using to get the last record is the following code:
db.Order.Max(o => o.OrderID);
I'm not sure that's the proper way to get the last record since my fear if there are mutiple users using the page at once it will be pulling the wrong record.
The original object will be updated with the ID after the SaveChanges is run.
Let's say you have this simple model
public class Person
{
public int PersonID { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public DateTime DOB { get; set; }
}
You new up an object
Person newperson1 = new Person { Forename = "Joe", Surname = "Bloggs" };
You then add it to the context and SaveChanges
using (MyContext context = new MyContext())
{
context.People.Add(newperson1)
context.SaveChanges();
}
Now that you have saved the changes EF will have handled the insertion and resulting ID value. You can simply access the newperson1.PersonID to get the returned ID. No need to requery for it.
Ok, Steve what I'm confused about is where did you get the "People" class to use to "add"? Is your mycontext your db context class?
MasterV23
Member
114 Points
327 Posts
Retrieving data back from specific column after SaveChanges()
Feb 21, 2013 02:29 PM|LINK
I'm trying to determine what is the correct way to save the data to the database and return data from a column after it saves (identity index column) back to be used to save data in another table.
Should the step be to save the data in the main table 1st and then query it for the index that was created to get the value and be used to save in another column within another table?
Example, order header information is the table the 1st table that would be saved and when saved it creates a new OrderID from identity, I'll need that order number to be used in the OrderItems table that holds the order items.
aaa_78600
Participant
942 Points
198 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 22, 2013 02:38 AM|LINK
after inserting 1st table retrieve
orSCOPE_IDENTITYsee below linkhttp://www.codeproject.com/Articles/103610/Difference-between-IDENTITY-SCOPE_IDENTITY-IDENT_C
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
MasterV23
Member
114 Points
327 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 22, 2013 03:01 AM|LINK
aaa_78600
Participant
942 Points
198 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 22, 2013 03:26 AM|LINK
http://sqlite.phxsoftware.com/forums/t/1420.aspx
http://www.codeproject.com/Questions/109505/Get-Record-ID-in-Entity-Framework-after-insert
MasterV23
Member
114 Points
327 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 25, 2013 03:04 PM|LINK
I guess I'm not understanding how the examples you showed work. I'm using EF's add then saving it.
What I'm currently using to get the last record is the following code:
I'm not sure that's the proper way to get the last record since my fear if there are mutiple users using the page at once it will be pulling the wrong record.
MasterV23
Member
114 Points
327 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 26, 2013 03:34 PM|LINK
Does anyone have any other suggestions or a explaination on how the examples in this thread work?
SteveJGordon
Member
118 Points
14 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 26, 2013 04:01 PM|LINK
The original object will be updated with the ID after the SaveChanges is run.
Let's say you have this simple model
public class Person { public int PersonID { get; set; } public string Forename { get; set; } public string Surname { get; set; } public DateTime DOB { get; set; } }You new up an object
Person newperson1 = new Person { Forename = "Joe", Surname = "Bloggs" };You then add it to the context and SaveChanges
using (MyContext context = new MyContext()) { context.People.Add(newperson1) context.SaveChanges(); }Now that you have saved the changes EF will have handled the insertion and resulting ID value. You can simply access the newperson1.PersonID to get the returned ID. No need to requery for it.
MasterV23
Member
114 Points
327 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 27, 2013 01:30 AM|LINK
SteveJGordon
Member
118 Points
14 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 27, 2013 06:19 AM|LINK
Yes, MyContext would be a DBContext and "People" would be a DBSet defined for the Person class.
The important part here is that after adding and saving an object you can just call it's ID proprerty which will return the new ID to you.
MasterV23
Member
114 Points
327 Posts
Re: Retrieving data back from specific column after SaveChanges()
Feb 27, 2013 05:46 PM|LINK
Thanks for clearing things up. I now fully understand.