Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Data is not inserting into DB, How handle composite primary key, where one PK is set to auto-increment. Here not entering any data into Id column , but it throws following exception .

"Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF."


What I have tried:

Table 
Id int primary key (auto increment)
SrNo varchar(6) primary key
Name varchar(10)

<pre lang="c#">   this.HasKey(m => new { m.Id, m.SrNo });

public bool Add(BusinessEntity mod)
                   {  _itemWarehouse.Insert(mod);   }


taking data from WinForm where
Id=0 (didn't set, default), SrNo = X12 , Name = ABC

But not entering data into DB , throwing exception

"Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF."
Posted
Updated 18-Jul-18 8:29am
Comments
MadMyche 18-Jul-18 13:57pm    
What type of DB?

1 solution

A table cannot have more than one "primary key".

EF can only recognize one primary key; either a single value or a compound key.

Anything else would be a "secondary key / index"
 
Share this answer
 
Comments
Member 12852443 19-Jul-18 5:21am    
thank u i got solution ,also i'm using here 2 columns in one Pk (multiple columns are allowed to use as PK), its working ,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900