Entity Framework - "An error occurred while updating the entries. See the inner exception for details" [duplicate]
Asked Answered
G

10

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.sstatic.net/6ckIF.png

Gaiseric answered 13/6, 2014 at 22:59 Comment(5)
What is the inner error?Clipped
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.Himelman
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.Waterborne
just an other solution: for me, it was because the length of string was greater then the varchar length of DB column.Reciprocity
In my case, it was column name miss-match between database table and entity model!Lifelong
R
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” (...)

Resistance answered 13/6, 2014 at 23:8 Comment(4)
Where should i turn it ON?Gaiseric
Unfortunately I have the Pluralization enabled so that's not the problem :/Gaiseric
@Gaiseric I suggest you create the tables with singular names, and then on your code, force singular names also, like shown in the image above. give it a try.Dactylogram
but in case if we are using identity ?can you guideBinns
I
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.

Ingrain answered 29/4, 2016 at 15:48 Comment(5)
This is what it was for me.Malmsey
Helpful suggestion to look for InnerException, got exact message as Entity name mismatch. Table name in database and Entity defined had different nameUptown
what does " in watch " mean?Karelian
Ah, the QuickWatch exception popup in visual studio.Karelian
this one helped me too!Circadian
K
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);
    }
Karelian answered 2/11, 2019 at 21:36 Comment(3)
Getting the reason of a DbUpdateException requires more than that.Centrifugate
Getting the reason of "An error occurred while updating the entries. See the inner exception for details” doesn't.Karelian
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 thatVenation
A
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......

Afrikander answered 20/3, 2018 at 21:16 Comment(0)
A
4

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

Andriette answered 7/6, 2018 at 2:40 Comment(0)
C
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!

Coping answered 3/3, 2018 at 14:17 Comment(0)
H
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.

Harmony answered 13/10, 2019 at 13:18 Comment(0)
C
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

Crummy answered 24/10, 2017 at 10:50 Comment(1)
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.Coping
V
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.

Voracity answered 6/12, 2017 at 12:28 Comment(0)
H
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)

Hadria answered 19/11, 2018 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.