Shafi Shaikh
Shafi Shaikh

Reputation: 161

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

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.

Upvotes: 5

Views: 31505

Answers (4)

Nikhil patil
Nikhil patil

Reputation: 11

check primary key in the table if not then set primary key to the id and identity specification to "is identity to YES"

Upvotes: 1

Debendra Dash
Debendra Dash

Reputation: 5596

In entity framework if you mentioned any field as key then it should have the Isidentity property as true,otherwise it will throw this erroe.

open SQL Management Studio you have to open the table that causes your problem and select the primary key(As mentioned in model). Looking at its property you will find the "Identity Specification". You have to set it to "yes".

Upvotes: 0

silviagreen
silviagreen

Reputation: 1729

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

Upvotes: 4

Raja Nadar
Raja Nadar

Reputation: 9489

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.

Upvotes: 3

Related Questions