this my class,
public class PatReg { [DatabaseGenerated(DatabaseGeneratedOption.Computed), ScaffoldColumn(false)] public Int64 RecId { get; set; } [Key,Display(Name = "File Id"), ScaffoldColumn(true), DatabaseGenerated(DatabaseGeneratedOption.None )] public Int64 FileId { get; set; } [Required, Display(Name = "First Name")] public string FName { get; set; } [Required, Display(Name = "Middle Name")] public string MName { get; set; } [Required, Display(Name = "Last Name")] public string LName { get; set; } [Required, Display(Name = "Date of Birth"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime Dob { get; set; } }
"FileId" is my primary key and I want to generate it upon saving the record and save it along with the record,
Custom number would have the following spec, YYMMDD0001 where YY is two digit of the year, MM Two digit of the month. DD is two digits of day, 001 is the serial start and reset every day.
This is my controller
// POST: PatReg/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("FileId,FName,MName,LName,Dob")] PatReg patReg) { if (ModelState.IsValid) { _context.Add(patReg); await _context.SaveChangesAsync(); return RedirectToAction("Index"); } return View(patReg); }
Background
I used to generate this number using SQL stored procedure like below,
Set @YY = (RIGHT(CONVERT(VARCHAR(8), GETDATE(), 1),2)) Set @MM = SUBSTRING(CONVERT(nvarchar(6),getdate(), 112),5,2) Set @DD = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, GETDATE())), 2) Set @SRL = (SELECT FileNumSrl FROM SetTblSrls WHERE RecID = 1)
SET @FileId =(select CAST(CONCAT ( @YY , @MM , @DD,00 ,@SRL) AS int))
"@SRL" represents the serial sequence that I lookup from "SetTblSrls" and I used to have a trigger on the target table to update this number on each insert by which i get a new number every time I generate the FileId
EDIT
How can I do it using C# and save it with the record