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












0














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;









share|improve this question
























  • 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












  • 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
















0














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;









share|improve this question
























  • 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












  • 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














0












0








0







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;









share|improve this 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;






c# .net-core entity-framework-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 1:36

























asked Dec 27 '18 at 20:12









FlyingV

425413




425413












  • 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












  • 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


















  • 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












  • 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
















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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer





















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


    }
    });














    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer


























      2














      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.






      share|improve this answer
























        2












        2








        2






        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.






        share|improve this 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 '18 at 20:56









        Imantas

        651315




        651315






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas