KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a MVC web application with a table in the model that I would like to add to. I have the primary key set along with the other data fields, but every time I try to add to the table, I get the following error: "Cannot insert explicit value for identity column in table 'TABLE_NAME' when IDENTITY_INSERT is set to OFF." I'm not sure why this problem is coming up, I have the primary key set as the identity and it is also set to auto increment in the Visual Studio table designer. Is there any way I can adjust the IDENTITY_INSERT parameter in the table designer in Visual Studio?? Or is there some other issue that might be causing this. UPDATE: @Brian - As far as I can tell, I'm not setting the value explicitly, here is the code that adds to the table(s). //Add viewer public void addViewer(ModelStateDictionary modelState, Users user) { var userToAdd = new UserRoles(); userToAdd.Users = user; if (String.IsNullOrEmpty(userToAdd.Users.Username)) { modelState.AddModelError("noName", "Please enter a username for the new Viewer"); } //See if Committee Member already exists try { userToAdd = _db.UserRoles.First(ur => ur.Users.Username == userToAdd.Users.Username); modelState.AddModelError("userExists", "A Viewer with that username already exists in the system"); return; } catch (Exception e) { if (modelState.IsValid) { //Assign Committee Member role userToAdd.Role = "Viewer"; userToAdd.Users = user; //Add new Committee Member to User Roles and associated username to Users _db.AddToUserRoles(userToAdd); _db.SaveChanges(); } } }
Tags (comma-separated)
Save Edits
Cancel