none
EF 6 - saveChanges() is saving unchanged records RRS feed

  • Question

  • I don't know if I'm missing something... anyway:

    It seems the 'saveChanges()'  is trying to add in my database some entities that have the 'Unchanged' state. I don't think that's correct. For example:

    You can see, for example, that the object with the property 'HomeTeam' = ' Forest Green Rovers' has the state = 'Unchanged'. Anyway all entities are 'unchanged' in my case. So, if I'm correct, the saveChanges shouldn't try to insert those in my table but that's what it's happening:

    and:

    The primary key has been violated, but EF shouldn't have tried to add this record (the one with the property HomeTeam = 'Forest Green Rovers') since it's  'Unchanged', right?

    Why is EF doing that? 

    Wednesday, September 13, 2017 2:43 PM

Answers

  • Found my error. That was stupid. Tomorrow I will post my mistake ;-)

    Edit: 

    simply didn't see there were a part in my code (I forgot to delete it) where I was adding in the context.MatchInfoes every objects without checking the PK constraints. I was focusing on the fact my entities had the 'Unchanged' state: anyway I guess the temp variable (first picture I posted) refers the entities the framework got from the database (during some read operations) and not the ones I was adding.


    Thanks for your support.

    • Edited by Mond089 Friday, September 15, 2017 6:39 AM Explained what solved my problem.
    • Marked as answer by Mond089 Friday, September 15, 2017 6:39 AM
    • Unmarked as answer by Mond089 Thursday, September 21, 2017 10:10 AM
    • Marked as answer by Mond089 Friday, September 22, 2017 10:41 AM
    Thursday, September 14, 2017 7:24 PM

All replies

  • Hi Mond089,

    Thank you for posting the problem here.

    >>You can see, for example, that the object with the property 'HomeTeam' = ' Forest Green Rovers' has the state = 'Unchanged'. Anyway all entities are 'unchanged' in my case. So, if I'm correct, the saveChanges shouldn't try to insert those in my table but that's what it's happening: 

    According to the error message, you violate the primary key constraint so the saveChanges method got the exception.

    As we know, if the SaveChanges method got an exception, your entities weren’t inserted into your table. But the above seems that your entities were added into your database when you violated primary key constraint when you . Form your second picture, it indicates that your insert statement has been terminated so your entities can’t be inserted into your table.

    There is a link which lists all exceptions about the Savechanges method, you could resolve your problem according to your exception.

    https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.savechanges(v=vs.113).aspx

    >> Why is EF doing that?

    There is a bug in your code. From  the information you provided, I didn’t find the code which you added some entities into your table by DbContext. If you could provide some code to reproduce your problem, I will help you better and faster.

    You could debug your code to check the code which inserts your entities into your table and compare whether the entities are the same as your data in your database.

    Best regards,

    Bob.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 14, 2017 7:16 AM
  • Hi,

    thanks for your reply. Anyway:

    - the table contains already some data. What I'm doing is:

    if (oMatchInfo != null)
    {
        oMatchInfoTemp = (from m in context.MatchInfoes where m.HomeTeam == oMatchInfo.HomeTeam && m.AwayTeam == oMatchInfo.AwayTeam && m.Date == oMatchInfo.Date select m).FirstOrDefault<MatchInfo>();
        if (oMatchInfoTemp == null)
        {  
             context.MatchInfoes.Add(oMatchInfo);
        }
        else
        {                                         
            context.OddsInfoes1X2.AddRange(oMatchInfo.OddsInfoes1X2); 
        }
    }

    Before trying to insert in the context.MatchInfoes I'm checking if the row is already present in my table. If not I'm gonna write in the context:

    context.MatchInfoes.Add(oMatchInfo);

    Maybe I'm wrong but what I know about saveChanges() is that only the entities in the Added state are inserted in the database but the row that's launching the exception has the Unchanged state, so should be ignored (as shown in the previous pictures). 

    It's something similar to:

    https://stackoverflow.com/questions/13808450/entity-framework-savechanges-is-saving-unchanged-record-via-stored-procedure-ma

    (I don't know if it's allowed post external link. Maybe someone will delete it).

    Thursday, September 14, 2017 10:09 AM
  • Found my error. That was stupid. Tomorrow I will post my mistake ;-)

    Edit: 

    simply didn't see there were a part in my code (I forgot to delete it) where I was adding in the context.MatchInfoes every objects without checking the PK constraints. I was focusing on the fact my entities had the 'Unchanged' state: anyway I guess the temp variable (first picture I posted) refers the entities the framework got from the database (during some read operations) and not the ones I was adding.


    Thanks for your support.

    • Edited by Mond089 Friday, September 15, 2017 6:39 AM Explained what solved my problem.
    • Marked as answer by Mond089 Friday, September 15, 2017 6:39 AM
    • Unmarked as answer by Mond089 Thursday, September 21, 2017 10:10 AM
    • Marked as answer by Mond089 Friday, September 22, 2017 10:41 AM
    Thursday, September 14, 2017 7:24 PM
  • Hi,

    I am glad to know you solved the problem. we appreciate you will share your solution, we are waiting for your feedback. After you share us your solution, please mark it as an answer. Thanks.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 15, 2017 2:28 AM