Im having problem with my code. Im inserting to my database using an uploaded comma delimeted (CSV) file which has 5 row in it. the first row successfully inserted to the DB but in the second loop to insert the second row i encounter an error:
Cannot insert explicit value for identity column in table 'bdoToDbs' when IDENTITY_INSERT is set to OFF.
Here is my Loop Codes
while (i < users.Count)
{
ViewBag.CompanyCode = users[i].CompanyCode;
ViewBag.ProductCode = users[i].ProductCode;
ViewBag.TransactionDate = users[i].TransactionDate;
ViewBag.TransactionTime = users[i].TransactionTime;
ViewBag.OriginatingBranch = users[i].OriginatingBranch;
ViewBag.CustomerNumber = users[i].CustomerNumber;
ViewBag.TransactionOrigin = users[i].TransactionOrigin;
ViewBag.TypeOfPayment = users[i].TypeOfPayment;
ViewBag.CheckNumber = users[i].CheckNumber;
ViewBag.TransactionAmount = users[i].TransactionAmount;
ViewBag.CustomerName = users[i].CustomerName;
bdoToDb.CompanyCode = ViewBag.CompanyCode;
bdoToDb.ProductCode = ViewBag.ProductCode;
bdoToDb.TransactionDate = ViewBag.TransactionDate;
bdoToDb.TransactionTime = ViewBag.TransactionTime;
bdoToDb.OriginatingBranch = ViewBag.OriginatingBranch;
bdoToDb.CustomerNumber = ViewBag.CustomerNumber;
bdoToDb.TransactionOrigin = ViewBag.TransactionOrigin;
bdoToDb.TypeOfPayment = ViewBag.TypeOfPayment;
bdoToDb.CheckNumber = ViewBag.CheckNumber;
bdoToDb.TransactionAmount = ViewBag.TransactionAmount;
bdoToDb.CustomerName = ViewBag.CustomerName;
bdoToDb.UserName = ViewBag.DisplayName;
bdoToDb.UserIP = HttpContext.Connection.RemoteIpAddress.ToString();
bdoToDb.UserDate = DateTime.Now.ToString("MM/dd/yyyy");
i++;
_context.Add(bdoToDb);
await _context.SaveChangesAsync();
});
return RedirectToAction(nameof(Index));
}