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.
- problem trying insert null value primary key column i.e emp_main_id.
- primary key doesn't allow insert null values or duplicate values.
- if want insert null values remove primary key constraint.
Comments
Post a Comment