I have a table called Quotations and it has an unique string field called QuotationNo.
public class Quotation
{
[Key]
public int Id { get; set; } //PK
public string QuotationNo{ get; set; } // Pattern QUOT/12-22/00001 => QUOT + MONTH-YEAR + PrimaryKey.ToStrin("5d");
}
So I have implemented it by calling _db.SaveChanges() method twice as follows.
if (ModelState.IsValid)
{
_db.Quotations.Add(quot);
_db.SaveChanges(); // 1 time saving
quot.QuotationNo= "QUOT/" + DateTime.Today.ToString("dd-MM/") + quot.Id.ToString("D5");
_db.Quotations.Update(quot);
_db.SaveChanges(); // 2 time saving
}
Is there a short way to do this?