Answer the question
In order to leave comments, you need to log in
What's wrong with Entity Framework Core?
Good day, dear ones. Help. Everything worked fine, but an error like
"SqlException: Cannot insert an explicit value for the identity column in the table "Deals" when the IDENTITY_INSERT parameter is set to OFF began to pop up.
"
I noticed that the ID of the object has a minimum value of int32. In doing so, I pre-set it to zero.
for (int k =0; k < deals1.Length; k++) {
deals1[k].ParentDealID = db.Deals.Where(d => d.GUID == parentDealInfo.GUID).FirstOrDefault().DealID;
deals1[k].DealID = 0;
Console.WriteLine(JsonConvert.SerializeObject(deals1[k]));
Console.WriteLine("Сделка: {0}; ParentDeal: {1}", deals1[k].ChildNumber, deals1[k].ParentDealID);
db.Add(deals1[k]);
db.SaveChanges();
Thread.Sleep(5);
}
Answer the question
In order to leave comments, you need to log in
This happens when the DealID is manually changed to an entry that is already in the database.
Where did deals1 come from ? Sampling from a database context? If yes, then EntityFramework knows that these records are already in the database and tracks their changes. Accordingly, it does not allow changing their identifiers explicitly and overwriting them in the database again.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question