DotNet Core C# Concurrency Entity Framework (Duplicate Key Value unique Constraints)
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;
c# .net-core entity-framework-core
|
show 2 more comments
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;
c# .net-core entity-framework-core
What's the purpose ofVendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values forVendor vendorInstance
andAC acInstance
come from? For which table do you get the error?
– Imantas
Dec 27 '18 at 20:33
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the lineIncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?
– Imantas
Dec 27 '18 at 20:39
1
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02
|
show 2 more comments
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;
c# .net-core entity-framework-core
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;
c# .net-core entity-framework-core
c# .net-core entity-framework-core
edited Dec 28 '18 at 1:36
asked Dec 27 '18 at 20:12
FlyingV
425413
425413
What's the purpose ofVendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values forVendor vendorInstance
andAC acInstance
come from? For which table do you get the error?
– Imantas
Dec 27 '18 at 20:33
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the lineIncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?
– Imantas
Dec 27 '18 at 20:39
1
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02
|
show 2 more comments
What's the purpose ofVendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values forVendor vendorInstance
andAC acInstance
come from? For which table do you get the error?
– Imantas
Dec 27 '18 at 20:33
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the lineIncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?
– Imantas
Dec 27 '18 at 20:39
1
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02
What's the purpose of
Vendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values for Vendor vendorInstance
and AC acInstance
come from? For which table do you get the error?– Imantas
Dec 27 '18 at 20:33
What's the purpose of
Vendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values for Vendor vendorInstance
and AC acInstance
come from? For which table do you get the error?– Imantas
Dec 27 '18 at 20:33
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the line
IncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?– Imantas
Dec 27 '18 at 20:39
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the line
IncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?– Imantas
Dec 27 '18 at 20:39
1
1
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02
|
show 2 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53950373%2fdotnet-core-c-sharp-concurrency-entity-framework-duplicate-key-value-unique-con%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 27 '18 at 20:56
Imantas
651315
651315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53950373%2fdotnet-core-c-sharp-concurrency-entity-framework-duplicate-key-value-unique-con%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the purpose of
Vendor vendorInstancer = vendorInstance;
? Is it supposed to copy the instance? Could you also include the code of your entities to see how they are setup? Where do values forVendor vendorInstance
andAC acInstance
come from? For which table do you get the error?– Imantas
Dec 27 '18 at 20:33
Its just a new instantiantion of Vendor; new Vendor(). I made it easier for you, by changing the code.
– FlyingV
Dec 27 '18 at 20:34
have you tried calling context.SaveChanges(); once at the end?
– saj
Dec 27 '18 at 20:36
Can't remember for sure, but don't you need to reload the entity after save to get the values saved to the database? That is, is the line
IncidentId = newIncidentToAdd.IncidentID
really getting you the correct IncidentID rather than default int value?– Imantas
Dec 27 '18 at 20:39
1
... what happened to the code?
– Ann L.
Dec 28 '18 at 2:02