Reputation: 605
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; }
}
Upvotes: 4
Views: 2825
Reputation: 5764
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>()
.
Upvotes: 3
Reputation: 605
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
Upvotes: 1
Reputation: 1946
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
Upvotes: 1