I have POCO objects which their identifiers are unique and generated automatically by the database, so the problem is when you want to know for some reason which will be the next identifier that the database is going to assign to the next record you are inserting. As far as I know it is only possible after performin dbContext.SaveChanges() so I would like to know if I am right or is there a way to know the next identifier assigned by database automatically.
Asked
Active
Viewed 146 times
0
-
This isn't so much an entity framework question as it is a general database design and SQL Server question. I would check out some related questions: http://stackoverflow.com/questions/562578/how-do-you-tell-what-next-identity-column-will-be – Justin Helgerson Jun 06 '13 at 19:10
2 Answers
1
is there a way to know the next identifier assigned by database automatically
Well, the next one NO. And if your code depends on it, you really need to change your design.
If you need the identifier to insert related objects, you should check some other questions because you can assign entities to eachother instead of ID's and it will be fine.

Pleun
- 8,856
- 2
- 30
- 50
-
Yes, I know. But I am interested only in the entity I am currently inserting, not its related ones. – Willy Jun 06 '13 at 19:11
-
-
It depends,in my case is necessary.I have a POCO object with some columns, Id (unique Identifier assigned by database) and some others.One of those column is a reference that is the same as Id but formatted as "0000001" instead of 1 so I want to know which is the next Id that database is generating because in this way, when creating the new record I can assign it to reference column and then perform one commit and avoiding to do 2 commits:first commit to obtain the id generated automatically by db and the assign id to the reference column formatted as "0000001" and do again another commit. – Willy Jun 06 '13 at 19:19
-
1@user1624552 in your example you could make the "other" column a **computed** column – qujck Jun 06 '13 at 19:20
-
so to do reference column as computed from id column, right? so when entity framework does the commit it automatically generates the id column and then it assigns id column formatted as "0000001" to the reference column, right? I am interested in doing it, so could you provide an example, this is just what I want. – Willy Jun 06 '13 at 19:23
-
Seems your POCO objects are not that POCO. In normal EF or Linq2SQL You simplu assign the entities to eachother and EF will take care of the references in one commit (one db.saveChanges()) – Pleun Jun 06 '13 at 19:23
-
Yes, I know, and this is what I am doing. I do not assign ids, I assign objects (entities) and then EF do the rest automatically, it assign automatically references, I know. – Willy Jun 06 '13 at 19:24
-
-
-
I have used a calculated column for reference. Something like this: http://geekswithblogs.net/DavidPaquette/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx – Willy Jun 06 '13 at 20:15
0
I agree with the general purport of the comments that having to know an identity value is "smelly". But on the other hand sometimes you have to live with a given design.
You can't really get the value of the next id, but you can get the value of the assigned id in time by using a TransactionScope
.
using (var trans = new TransactionScope())
{
// Create new object
...
context.SaveChanges();
int id = newEntity.Id;
dependentEntity.IdString = string.Format("{0:0000000}", id);
context.SaveChanges();
trans.Complete();
}

Gert Arnold
- 105,341
- 31
- 202
- 291