4

Im using MVC6 project with Asp.net Identity and wanted to change the ID column from the string to INT. I Followed this article enter link description here

I get an error saying can insert a null into the ID columns for Role and User, but if i revert back to the norm it works.

public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<PropertyManagementCompany> PMC { get; set; }
}
Loren.Dorez
  • 605
  • 1
  • 8
  • 11

3 Answers3

3

Here is how to use integer column on Identity, Asp NET Core & Entity Framework Core:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

public class AppDbContext : IdentityDbContext<User, Role, int>
{
    public AppDbContext(DbContextOptions options) : base(options) {}
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<User, Role>(options => {
            // ...
        }).AddEntityFrameworkStores<AppDbContext, int>(); // NOTE this line
    }
}

If you get odd runtime error:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'TUser'.

It means you forgot to add the TKey on the AddEntityFrameworkStores<AppDbContext, int>().

Ciantic
  • 6,064
  • 4
  • 54
  • 49
  • I was getting errors running `update-database` due to foreign key constraints. Deleting the migrations folder & database and then creating a new initial migration (`add-migration Initial`) fixed it. Though, this won't really work if you already have other things in your database / migrations folder (i.e. it isn't a new project). – Richard Marskell - Drackir Sep 12 '16 at 13:26
1

I did that for MVC5 before below is what I did actually

#region Entities

public class ApplicationUserClaim : IdentityUserClaim<Int32> { }
public class ApplicationUserRole : IdentityUserRole<Int32> { }
public class ApplicationUserLogin : IdentityUserLogin<Int32> { }
public class ApplicationRole : IdentityRole<Int32, ApplicationUserRole> { }
public class ApplicationUser : IdentityUser<Int32, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUser<Int32> { }
public class ApplicationClaimsPrincipal : ClaimsPrincipal
{
    public ApplicationClaimsPrincipal(ClaimsPrincipal claimsPrincipal) : base(claimsPrincipal) { }
    public Int32 UserId { get { return Int32.Parse(this.FindFirst(ClaimTypes.Sid).Value); } }
}

#endregion

#region Stores

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, Int32, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore() : base(new CustomsSiteDbContext()) { }
    public ApplicationUserStore(CustomsSiteDbContext context) : base(context) { }
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, Int32, ApplicationUserRole>
{
    public ApplicationRoleStore() : base(new CustomsSiteDbContext()) { }
    public ApplicationRoleStore(CustomsSiteDbContext context) : base(context) { }
}

#endregion

#region Managers

public class ApplicationUserManager : UserManager<ApplicationUser, Int32>
{
    public ApplicationUserManager() : base(new ApplicationUserStore()) { }
    public ApplicationUserManager(ApplicationUserStore userStore) : base(userStore) { }
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, Int32>
{
    public ApplicationRoleManager() : base(new ApplicationRoleStore()) { }
    public ApplicationRoleManager(ApplicationRoleStore roleStore) : base(roleStore) { }
}

#endregion

I hope this helps

Sherif Ahmed
  • 1,896
  • 1
  • 19
  • 37
  • It seems with ASP.net 5 and MVC 6 this is suppose to be easier like the Article i linked to. Ill try this code later and see – Loren.Dorez Oct 29 '15 at 21:13
1

I found the issue. It had to do with Migrations. When VS2015 first created the project from the template it already as a migration build with the column as string even if i changed the code to INT in my original post. So what you need to do is the following

  1. Delete You Migrations and ASP.Net Identity Database Tables
  2. Delete the files in the Migrations folder in your project
  3. Run a new migration command "dnx ef migrations add "
  4. Next time try to access Identity it will rebuild the tables with the correct PK Types
Loren.Dorez
  • 605
  • 1
  • 8
  • 11