DotNet Core C# Concurrency Entity Framework (Duplicate Key Value unique Constraints)

.net-core c# entity-framework-core

Question

TL&DR: Several concurrent Tasks trying to place identical records into a database; Essentially SEVERAL tasks are being spun up and opening up several files that could be identical.

It is vital to save all the information, in a heavily nested table, based on the IP Address; Here is what I have tried so far in the last 4 days of work (even during Christmas!)

  • Tried to use a Transaction, within a do while() loop (with context.Rollback(). [Didn't work!]
  • Tried to Put Random Sleeps within each of the Inserts to Stop Race Condition [Didn't work!]
  • Made Code no longer Asyc . [Didn't work!]
  • Current algorithm doesn't work and peg's CPU! [Doesn't work!]\
  • Seperately add EACH object to the Table individually [Didn't work!]

Each of the Objects increments, during insert. This is why this doesn't make sense. I am at at a loss of words.

Object Relationships

IP has many Incidents;
1
0
12/28/2018 1:36:32 AM

Accepted Answer

I think you might have a problem in these lines:

Vendor vendorInstancer = new Vendor();
vendorInstance.IncidentID = IncidentId;
context.Vendors.Add(vendorInstancer);

Note the variable names. You create vendorInstancer but update ID of the vendorInstance. That is, not the entity you're saving to the database. Hard to spot that one letter difference.

2
12/27/2018 8:56:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow