D
D
DenisVladimirovich2019-09-02 13:02:02
ASP.NET
DenisVladimirovich, 2019-09-02 13:02:02

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);
                            }

5d6ce5d63ca2a399100305.png
Or maybe I'm missing something in the indexes. If I didn't explicitly set it, then it should sort of install itself, and EFCore seems to take care of it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Egorov, 2019-09-02
@SergeyEgorov

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 question

Ask a Question

731 491 924 answers to any question