I want to save changes to the SQL table in a loop. I have the following code:
string[] request={"test1", "test2", "test3"};
TransactionData TTD = new TransactionData();
using ( IdContext = new ProofContext())
{
foreach(var req in request)
{
TTD.Description = req;
TTD.RecordId = 12;
TTD.PaymentStatus = "Failed";
IdContext.TransactionData.Add(TTD);
IdContext.SaveChanges();
}
}
I want to save three rows in the SQL table with three different ID. The table has one identity column and it is the primary key too. When I run my code, the changes are saved the very first time, but second time when I tried to save the changes, I got the error saying:
Cannot insert explicit value for identity column in table TransactionData when Identity Insert is set off.
I tried running this statement on SQL side in order to fix this error
SET IDENTITY_INSERT TransactionData ON
but still , I keep getting the error. This is the table model:
public partial class TransactionData
{
public int InfoId { get; set; } // this is the identity column in the database
public decimal UnitPrice { get; set; }
public decimal ServiceFee { get; set; }
public decimal TotalAmount { get; set; }
public string Sku { get; set; }
}
any help will be greatly appreciated