0

After I've followed this to change the type of Application User Id from string to int, I get SqlException if I try to create a new user.

The exact error is:

Cannot insert the value NULL into column 'Id' table 'DBNAME.dbo.AspNetUsers'; column does not allow nulls. INSERT fails. The statement has been terminated.

Line 208:                };
Line 209:
Line 210:                var result = await UserManager.CreateAsync(user, model.Password);
Line 211:                if (result.Succeeded)
Line 212:                {

Source File: C:\Projects\ProjectName\ProjectName\Controllers\MembersController.cs    Line: 210 

[SqlException (0x80131904): Cannot insert the value NULL into column 'Id', table 'DBNAME.dbo.AspNetUsers'; column does not allow nulls. INSERT fails.
The statement has been terminated.]

Here is the screenshot of AspNetUsers table design view: enter image description here

I've looked at How to tell the primary key 'Id' of IdentityUser class is IDENTITY(1,1)? and ASP.NET Identity - Error when changing User ID Primary Key default type from string to int AND when using custom table names but couldn't help me much.

Any help is very much appreciated.

Tonmoy
  • 2,008
  • 2
  • 10
  • 16

1 Answers1

0

Your problem that Identity is no longer generating a key for you - it did before.

For this problem to work you need to get the key automatically generated by a database. To get this done you need to apply the following attributes on your Id property in ApplicationUser class:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override int Id { get; set; }

And add another DB-migration to make sure the database knows what to do with this field.

UPD: oops. Just noticed that you already link to my identical answer. Does this not help?

trailmax
  • 34,305
  • 22
  • 140
  • 234
  • I did the same but nothing good happens, also the migration file shows empty. – Tonmoy Feb 07 '18 at 19:32
  • Migration might be a problem here. I remember I had to drop and re-create the table for the migrations to pick up on this change. Try [this method](https://stackoverflow.com/a/1049305/809357) to make your `Id` column to become `Identity(1,1)` – trailmax Feb 07 '18 at 21:04