c# - Insert value into non-identity key field : DB First -


ran small problem while working on ef 6 database first approach.

when try insert value key field of table not set identity, receive following exception:

cannot insert value null column 'emp_main_id',column not allow nulls. insert fails.
statement has been terminated.

i added following code in entity class avoid error

[key] [databasegenerated(databasegeneratedoption.none)] public int emp_main_id { get; set; } 

but still not working.

below script create db table

    create table [dbo].[employeesmain] (     [emp_main_id]          int            not null,     [emp_main_first]       nvarchar (40)  not null,     [emp_main_middle]      nvarchar (40)  null,     [emp_main_last]        nvarchar (40)  not null,     [emp_main_dob]         date           not null,     [emp_main_gender]      int            default ((1)) not null,     [emp_main_type]        int            default ((0)) not null,     [emp_main_email]       nvarchar (150) not null,     [emp_main_password]    nvarchar (max) not null,     [emp_main_designation] nvarchar (150) null,     [emp_main_skill]       nvarchar (max) null,     [emp_main_contract]    int            default ((0)) not null,     [emp_main_lead]        nvarchar (81)  null,     [emp_main_img]         nvarchar (max) null,     [emp_main_doj]         date           default ('01-jan-2012') null,     constraint [emp_main_primary_key] primary key ([emp_main_id]) ); 

any on appreciated.

  1. problem trying insert null value primary key column i.e emp_main_id.
  2. primary key doesn't allow insert null values or duplicate values.
  3. if want insert null values remove primary key constraint.

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -