An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Solution 1
This issue typically happens when you have a sub-type table mapping, but its primary key is set to auto-generate. this is problematic, since EF will be explicitly setting the value based on the key of the root entity.
if the above is the problem indeed, then to solve it, set the "Identity Specification
" of your child table to false. i.e. Remove the StoreGeneratedPattern="identity"
from the EDMX.
Solution 2
If cannot see any innerexception message that can help you (during debug), I have found the solution of the "Identity Specification" problem.
In SQL Management Studio (or in Server Explorer in Visual Studio) you have to open the table that causes your problem and select che primary key. Looking at its property you will find the "Identity Specification". You have to set it to "yes".
Source: http://www.technologycrowds.com/2015/07/system-data-entity-infrastructure-DbUpdateException.html
Solution 3
check primary key in the table if not then set primary key to the id and identity specification to "is identity to YES"
Related videos on Youtube
Shafi Shaikh
I am Full Stack Developer specialised in ASP.Net MVC and developer of MicroCRM, MicroLOGISTICS, MicroCMS web based applications built using ASP.Net MVC 5 and ASP.Net Core 2.0 Working on ASP.Net since last 10 years. My major key skills are C#, LINQ, Entity Framework Code First, MS Sql Server 2016. Completed almost 500 websites till date and 15 web applications projects including projects which I have mentioned above. I spend most of my time by working with ASP.Net, HTML, CSS, jQuery, Bootstrap, Javascript etc. I attend my customers, discuss with them about the projects, coordinate with my employees. I am passionate about learning new web technologies including front end and backend.
Updated on December 25, 2020Comments
-
Shafi Shaikh almost 3 years
I mapped two tables and created scaffolding in MVC. After filling data in the form when I click on create button, below error occurs.
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
-
Henk Holterman over 9 yearsShow the tables and their mapping. Also the updating code.
-
-
Shafi Shaikh over 9 yearsHow to set the "Identity Specification" of your child table to false. I searched everywhere in .edmx file but could not find this option. Can you please help me.