0

Hi I am trying to create a new customer record. The primary id customerid is autoincremental column.

When I try to insert a new record I get this error :

Message=Cannot insert explicit value for identity column in table 'Customer' when IDENTITY_INSERT is set to OFF.

This is the same code I am using :

NewCustomer.Name= user.Name;
NewCustomer.Address= user.Address;
NewCustomer.Phone = user.Phone;
NewCustomer.Email= user.Email;

db.Customers.Add(NewCustomer);
db.SaveChanges();

I read somewhere that I have to set autoincrement on, but I am using entity framework and not any direct sql. Can some one please help how I could turn on autoincrement so I could insert records?

tereško
  • 58,060
  • 25
  • 98
  • 150
kayze
  • 738
  • 8
  • 19
  • 33
  • Are you using SQL Server as your database? – Armand Sep 13 '13 at 13:32
  • Does entity framework know that the column is auto-increment? How did you create your models and your database? If you generated the models against the database before it was auto-increment and then later changed the database then you'd have to update the models, for example. – David Sep 13 '13 at 13:40
  • http://stackoverflow.com/questions/3188194/c-entity-framework-auto-increment-problem, http://stackoverflow.com/questions/14612813/entity-framework-code-first-using-one-column-as-primary-key-and-another-as-auto – Slavo Sep 13 '13 at 14:46

0 Answers0