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.Trackings.Add(tracking);
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?
ToPeString
does. \$\endgroup\$SaveChanges
fails the database content is inconsistent. For me, this is a Stack-Overflow question because the code must be fixed. \$\endgroup\$