2

I have a table with names 'Employee' with four field EmployeeId,Name,Address,Age I have set the EmployeeId is Primary key.I want that EmployeeId field will be incremented automatically whenever adding any new records.

Codebehind for insert is: { DataClassesDataContext db = new DataClassesDataContext();

    Employee emp = new Employee {Name = "James", Address = "India", Age = 24};
    db.Employees.InsertOnSubmit(emp);
    db.SubmitChanges();
    ShowEmployee();

}

designer.cs:

[Column(Storage = "_EmployeeId", DbType = "Int NOT NULL", IsPrimaryKey = true,IsDbGenerated=true,CanBeNull=false)]

When I am running this application getting the following error:

Cannot insert the value NULL into column 'EmployeeId', table 'Habib.dbo.Employee'; column does not allow nulls. INSERT fails. The statement has been terminated.

Any help is highly appreciated.

Thanks, Masum

Sander
  • 25,685
  • 3
  • 53
  • 85

1 Answers1

3

The EmployeeId field should be an Identity column in your database table. It will automatically increment if you do that. LINQ should pick up that it's an Identity column after that.

mkchandler
  • 4,688
  • 3
  • 23
  • 25
  • Hi.....in database table how to make identity column any idea?I have created a column name EmployeeId and marked as primary key. – Masuma Aktar Jan 15 '09 at 13:43
  • If you are working in SQL Server Management Studio, it will be in the Properties section for the table when in Design mode, the property is: "Identity Column", and you will choose your EmployeeId column from the list. – mkchandler Jan 15 '09 at 15:08
  • Just in case anyone is pulling their hair out over this like me see my answer here http://stackoverflow.com/a/27100918/74585 – Matthew Lock Nov 24 '14 at 08:51