locked
delete then save then add VS delete then save then create new DBContext then add then save... RRS feed

  • Question

  • Hi all,

    I've got a weird bug in some old code.

    code goes:

    if data exists for date X delete, SaveChanges() *(see below), add, SaveChanges()

    users say this was working and tested... I can't see how this ever worked as I can repeat the bug every time. The effect its having is that the users "load the data" (no problem), but then if they ever need to reload the system "load the data" says OK, but only the delete happens. So the data disappears but no error is thrown.

    what happens is if there is no data for that date then its fine, but if there is existing data, then the delete is occuring but the add never completes - the logs say its doing its thing but its too quick, ~450ms, as opposed to around 13000 when it actually works.

    * the catch is that the bug appears to be in teh "middle" saveChanges() where its actually calling a funciton called "discardAndRecreateConnection()" which does this:

    db = discardAndRecreateConnection(db);

    where

    DbContext discardAndRecreateConnection(DbContext old)
    {var newDb = CreateContext(old.ConnectionString);
    return newDb;
    }

    my questions are:

    a)with the deleting and re-adding of data is it necessary to do something like the above? if so how do you do it RIGHT...?!

    b)why does this appear to fail silently - what is going on that it doesn't error but the "adds" that follow the discard adn recreate never make it to a DB call...

    c) if you simply ditch the "discard" and purely did a "delete/savechanges()/add/savechanges()" are you goign to run into any issues?

    ********************************
    UPDATE - Found a comment in the change history as to why this code exists.

    With a large number of replacing data entries the memory would blow out, so looks like the "discard and recreate" was to attempt to force EF to release some memory... not sure if that helps at all

    ********************************


    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -




    • Edited by noJedi Monday, September 14, 2015 1:13 AM
    Sunday, September 13, 2015 11:58 AM

All replies

  • Hi Nojedi,

    >> a)with the deleting and re-adding of data is it necessary to do something like the above? if so how do you do it RIGHT...?!

    b)why does this appear to fail silently - what is going on that it doesn't error but the "adds" that follow the discard adn recreate never make it to a DB call...

    c) if you simply ditch the "discard" and purely did a "delete/savechanges()/add/savechanges()" are you goign to run into any issues?

    Are you trying to solve this problem by recreate a new db context using the same connection string? I think you are on the wrong direction to solve this problem. If you want to find why new data cannot insert into db, I would recommend you use SQL Server Profiler to monitor the executed sql statement which called by your app. It can log insert and delete statement sent by EF and you will know more about this issue.

    See more info about the profiler, https://msdn.microsoft.com/en-us/library/ms181091.aspx?f=255&MSPPError=-2147217396, try it and post more useful info about your problem.

    If you don't know how to do with the tool, can you post a repro sample, I will work on and let you know my result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 14, 2015 5:46 AM
  • Hi Herro. its taken me a long time to troubleshoot this - in particular as I'm not great with sql profiler - I find it a very difficult tool to use.

    Basically what it shows is;

    When there is NO existing data:

    Insert...
    Insert...
    Audit Logout
    Audit Login
    Update "job complete" <-- this is triggered by the code AFTER the bit i'm having trouble with
    Audit Logout
    exec sp_reset_connection

    When there is existing data:

    Delete...
    Delete...
    ***supposed to be INSERTS here... but there is no errors or warnings or notifications, its just "skipped"...
    Audit Logout
    Audit Login
    Update "job complete" <-- this is triggered by the code AFTER the bit i'm having trouble with
    Audit Logout
    exec sp_reset_connection

    The only extra info I can see is that the EF CODE is running (I can step through it) but NOTHING is happening in SQL profiler where the "Inserts" should be happening in in Scenrio B above...

    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -


    • Edited by noJedi Thursday, October 8, 2015 12:11 AM
    Thursday, October 8, 2015 12:02 AM
  • Thank you for explaining more about your scenario. But it's not enough for me to understand or troubleshoot. Can you post a repro project using your OneDrive?

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 8, 2015 2:03 AM
  • I have tried to include as much relevant structures as in my production code to give you the "big picture"...

    the only thing that MIGHT not be obvious is the "create()" on the DBcontext - there are multiple tenanted datbases doing this the create() in the real code takes a param "prefix" that gets the connection to the right instance... so its a little contrived, but illustrates the "dissappearance" of data without the system complaining.

    I suspect it might have something to do with either the usings/Idisposable codetimers or the way its doing the discard/recreate.

    If you can help me identify the issue that would be fantastic!

    https://onedrive.live.com/redir?resid=C1CAC662460F7839!99825&authkey=!AGbwSb_CV42ucws&ithint=file%2czip


    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -

    Wednesday, October 14, 2015 9:42 AM