# use Identity value in entity after insert in Database [closed]

i want to generate value using current date and ID of inserted entity
problem is that ID value not generated until insert object to database, so i use this code for generate value:

public ActionResult Create([Bind(Include = "ID,TrackingCode,FirstName,LastName,ProductName,CurrentStats,FailureReason,ReceiveDateTime,DeliveryDateTime,Price,Description,SerialNumber,ExtraItems,PhoneNumber")] Tracking tracking)
{
if (ModelState.IsValid)
{
db.SaveChanges();
Tracking _tracking = db.Trackings.Find(tracking.ID);
db.Entry(_tracking).State = EntityState.Modified;
_tracking.TrackingCode = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd") + tracking.ID.ToString());
db.SaveChanges();
return RedirectToAction("Index");
}

return View(tracking);
}


code work fine and do job that i want but i think this is not standard way is there any better approach to improve this code?

## closed as off-topic by t3chb0t, Graipher, alecxe, RobH, MastOct 18 '17 at 15:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – t3chb0t, Graipher, alecxe, RobH, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

• Off-topic: this is not a review task but a question how to solve a programming problem. Your current code doesn't work as intended. – Gert Arnold Oct 16 '17 at 7:31
• @GertArnold code work fine and do what i want , but i think this is not standard way – AminM Oct 16 '17 at 7:40
• problem is that ID value not generated until insert object to database So your code doesn't work the way you want it to work. You want it to do it all in one transaction. Also, it's not clear what ToPeString does. – Gert Arnold Oct 16 '17 at 7:46
• @GertArnold ToPeString is just extension Method – AminM Oct 16 '17 at 8:02
• @Vogel612 That's not my point. My point is: I would never say that the code works fine because if the second SaveChanges fails the database content is inconsistent. For me, this is a Stack-Overflow question because the code must be fixed. – Gert Arnold Oct 16 '17 at 10:25

I would suggest you change your database design.

Option 1)

Have SQL Server set a column to the current date/time when you insert a row See https://stackoverflow.com/q/6594589 for how to do it. Then have that column mapped on your Tracking entity but mark the property as [DatabaseGenerated(DatabaseGeneratedOption.Computed)]

Assuming you call the field AddDate

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]


Now you can create your get only TrackingCode property in code and use the [NotMapped] on it and define it as

[NotMapped]
public int TrackingCode
{
get { return Convert.ToInt32(AddDate.ToString("yyyyMMdd") + tracking;
}


Option 2:

Create an after insert tigger and have SQL Server update the TrackingCode. Again I would mark the property as DatabaseGenerated

In a nut shell it's better to offload this to the SQL Server

• why add DatabaseGeneratedOption.Computed to datetime?? i have problem with ID value – AminM Oct 17 '17 at 13:42
• That tells Entity framework to retrieve the field but don't update the field as the database will be the one to generate the value and not the entity. – CharlesNRice Oct 17 '17 at 13:43
• why Propery dont have setter and you just put getter only? – AminM Oct 17 '17 at 14:04
• IF you go with option 1 you have sql update a new column. I called it add date. Then you just make a calculated property to use in your code. If you actually want the value to be in a column in SQL then I suggest you do option 2 – CharlesNRice Oct 17 '17 at 14:06