25

I have problem, I have just started learning EF Model First and Im staying in one point for some time. I got such error :

"An error occurred while updating the entries. See the inner exception for details"

I have created an easy model on diagram, generated the database and wrote easy code in C# to add just one row in the table but the error is showing up all the time.

I post screenshot with Diagram/Generated DLL/Simple Main/And error throwing

model and stuff

Link for bigger size : https://i.stack.imgur.com/6ckIF.png

CSharpBeginner
  • 1,625
  • 5
  • 22
  • 36
  • 2
    What is the inner error? – TheNorthWes Jun 13 '14 at 23:03
  • 2
    `See Inner Exception for details` means what it says. You are calling an object `dbo.Pupils` that doesnt exist. Did you have EF pluralize your tables for you? If so, the EF table might be something odd like "Pupilss". Usually the database table is singular and EF is pluralized. And having the `dbo` in front of it is odd as well. – crthompson Jun 13 '14 at 23:05
  • 1
    Is your Connection string set up properly to point to that database? Where is your code for mapping your classes to the database? Please post code, and save the screenshots for visual cues as to what's going wrong. Just copying and pasting your Exception detail would be fine instead of a screenshot of that window. – krillgar Jun 13 '14 at 23:05
  • just an other solution: for me, it was because the length of string was greater then the varchar length of DB column. –  Mar 26 '19 at 10:06
  • In my case, it was column name miss-match between database table and entity model! – Khalid Bin Sarower Aug 13 '23 at 12:16

10 Answers10

21

Turn the Pluralization On. The problem is that you model object are using singular name (Pupil) convention, while in your database you are using pluralized names Pupils with s.

UPDATE

This post shows how can you turn it on or off. Some relevant excerpt of that post:

To turn pluralization on and off

  • On the Tools menu, click Options.

  • In the Options dialog box, expand Database Tools. Note: Select Show all settings if the Database Tools node is not visible.

  • Click O/R Designer.

  • Set Pluralization of names to Enabled = False to set the O/R Designer so that it does not change class names.

  • Set Pluralization of names to Enabled = True to apply pluralization rules to the class names of objects added to the O/R Designer.

UPDATE 2

But note that, you should avoid pluralized names. You can read here how to do it (I'll cite it here, just in case the link gets broken).

(...) When you work with Entity Framework Code First approach, you are creating your database tables from your model classes. Usually Entity Framework will create tables with Pluralized names. that means if you have a model class called PhoneNumber, Entity framework will create a table for this class called “PhoneNumbers“. If you wish to avoid pluralized name and wants singular name like Customer , you can do it like this In your DBContext class, Override the “OnModelCreating” method like this (...)

enter image description here

(...) Having this Method Overriding will avoid creating tables with pluralized names. Now it will create a Table called “PhoneNumber” , Not “PhoneNumbers” (...)

João Pinho
  • 3,725
  • 1
  • 19
  • 29
18

It could be caused by a data conversion from .NET to SQL, for instance a datetime conversion error. For me it was a null reference to a datetime column.

Also, that is not an exact error message. You can see the exact error in watch at exception.InnerException.InnerException -> ResultView.

Andrew
  • 18,680
  • 13
  • 103
  • 118
FARHAD AFSAR
  • 440
  • 5
  • 10
8

View the Inner Exception of the exception to get a more specific error message.

One way to view the Inner Exception would be to catch the exception, and put a breakpoint on it. Then in the Locals window: select the Exception variable > InnerException > Message

And/Or just write to console:

    catch (Exception e)
    {
        Console.WriteLine(e.InnerException.Message);
    }
Andrew
  • 18,680
  • 13
  • 103
  • 118
  • Getting the reason of a DbUpdateException requires more than that. – Gert Arnold Nov 02 '19 at 21:37
  • Getting the reason of "An error occurred while updating the entries. See the inner exception for details” doesn't. – Andrew Nov 02 '19 at 21:39
  • 1
    Thank you for showing me how to see InnerException. That takes me closer to the solution in my own case. I gave an upvote for that – Josh Dec 26 '19 at 06:39
4

For the records I had this issue and was a stupid mistake on my end. My issue was data type mismatch. Data type in database table and C# classes should be same......

4

My problem was that the Id of the table is not AUTO_INCREMENT and I was trying to add range.

M Fuat
  • 1,330
  • 3
  • 12
  • 24
2

I was facing the same problem and non of the above solutions helped me. In my Web Api 2 project, I had actually updated my database and had placed a unique constraint on an SQL table column. That was actually causing the problem. Simply Checking the the duplicate column values before inserting helped me fix the problem!

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
2

I had same problem about SaveChanges() in EF but in my case I forget to update my sql table then after I used migration my problem solved so maybe updating your tables will solve problem.

Shojaeddin
  • 1,851
  • 1
  • 18
  • 16
0

I faced the same error :

"An error occurred while updating the entries. See the inner exception for details”

Simply Delete and Recreate the *.edmx file. Its worked for me. the error will gone

user688
  • 361
  • 3
  • 22
  • This can't be a solution, if and only if it worked, in your case, then I would say it was a hack not a solution. – Jamshaid K. Mar 03 '18 at 14:15
0

I had this problem recently. This was happen, because the permissions of user database. check permissions of user database, maybe the user do not have permission to write on db.

Edvan Souza
  • 1,058
  • 9
  • 11
0

In my case.. following steps resolved:

There was a column value which was set to "Update" - replaced it with Edit (non sql keyword) There was a space in one of the column names (removed the extra space or trim)