Last post May 23, 2019 12:32 PM by PatriceSc
May 20, 2019 01:43 AM|ExceedingLife|LINK
I have the default identity asp.net database. I added some columnc/properties to my user. One of my columns use the IDENTITY property in my SQL. When I go to add a new user i get this error. 'Cannot insert explicit value for identity column in table
'AspNetUsers' when IDENTITY_INSERT is set to OFF.' I have migrations set and if I add a seeded user i get the same error. If I use Set IDENTITY tablename ON I still get this error. How can i do this with my entity framework setup on mvc5? Please and thank
var result = await UserManager.CreateAsync(user, model.Password);
May 20, 2019 07:04 AM|Brando ZWZ|LINK
Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF.'
As far as I know, this error is normally thrown when your code is trying to insert a specific value into the primary key column that is defined as IDENTITY.
I suggest you could try to avoid inserting the values which is related with the IDENTITY column. That means you shouldn't set the IDENTITY property vlaue in your mode when you want to insert it.
May 23, 2019 12:32 PM|PatriceSc|LINK
Usually it happens when EF doesn't know this is an identity column (and so rather than just ignoring this column, it tries to insert a value causing this error on the SQL Server side).
Make sure to use the [DatabaseGenerated(DatabaseGeeratedOption.Identity)] data annotation or the HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) fluent API so that EF knows about your identity column.