I am trying to insert a row to a table with an identity column and three "normal" columns using entity framework in wpf. however i got this error message: Cannot insert explicit value for identity column in table 'MyTable' when IDENTITY_INSERT is set to OFF here is my code snippet, trying to add t开发者_StackOverflowo the user table who has name, surname, age and userID(identity) column:
var newuser = new User();
newuser.SurName = surNameTextBox.Text;
newuser.Name = nameTextBox.Text;
newuser.Age = Int32.Parse(ageTextBox.Text);
context.AddToUsers(newuser);
context.SaveChanges();
Can anyone help me on solving this problem?
I'm not familiar with entity-framework, but for the curious this is the MS SQL command for enabling an insert on an identity column:
SET IDENTITY_INSERT tablename ON
I agree with Wonko to Sane, the problem is almost certainly in the AddToUsers method.
Identity columns indeed update themselves - that is their purpose.
If this is the only code that you are using to update the User, then I would look at (or post) the AddToUsers method in the DataContext.
精彩评论