InvalidOperationException when calling SaveChanges in .NET Entity framework

后端 未结 8 1226
小蘑菇
小蘑菇 2020-12-03 17:22

I\'m trying to learn how to use the Entity framework but I\'ve hit an issue I can\'t solve. What I\'m doing is that I\'m walking through a list of Movies that I have and ins

相关标签:
8条回答
  • 2020-12-03 17:51

    if you have a bindingsource bound, you should call the suspendbinding:

            bs.SuspendBinding();
            Data.SaveChanges();
            bs.ResumeBinding();
    
    0 讨论(0)
  • 2020-12-03 18:01
    System.Data.Objects.SaveOptions.None
    

    This solves the problem but I'm wondering what's the difference between uning none or the other options.

    0 讨论(0)
  • 2020-12-03 18:03

    Check if Primary Keys are missing in any of your etities, if yes, then create them, and do the "UpdateModel from Database"

    this should Solve the problem

    0 讨论(0)
  • 2020-12-03 18:04

    I found the solution to this.

    What happened was that when I created my table I forgot to add the primary key and set (Is Identity) property to yes. I then created my Entity model and got this error.

    I went back and fixed my database table but I still hade the weird Exception. What solved the problem in the end was to remove the entity and re-create it after the table was fixed.

    No more exceptions :)

    0 讨论(0)
  • 2020-12-03 18:07

    I had the similar issue with table relationship spanning three levels Like Customer->Order->Order-Details with oracle 12C Auto-ID Trigger for key generation. Insertion using SaveChanges() was throwing System.InvalidOperationException.

    Solution: Augmenting function SaveChanges(System.Data.Objects.SaveOptions.None) prevents system to use temporary EntityKeys in state of object graph and in result exception is avoided.

    0 讨论(0)
  • 2020-12-03 18:08

    Also Check if you set the "Is Identity" property for another column to "yes" and you are inserting a duplicated values into it.

    0 讨论(0)
提交回复
热议问题