ASP.Net 使用 INT id 列的 MVC 6 标识

ASP.Net Identity to MVC 6 with INT id Column

我正在使用具有 Asp.net Identity 的 MVC6 项目,并希望将 ID 列从字符串更改为 INT。我关注了这篇文章 enter link description here

我收到一条错误消息,说可以在角色和用户的 ID 列中插入一个空值,但如果我恢复到正常状态,它就可以工作。

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; }
}

我之前为 MVC5 做过,下面是我实际做的

#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

希望对您有所帮助

我发现了问题。它与迁移有关。当 VS2015 首次从模板创建项目时,它已经作为一个迁移构建,其中列为字符串,即使我在原始 post 中将代码更改为 INT。所以你需要做的是以下

  1. 删除您的迁移和 ASP.Net 身份数据库表
  2. 删除项目中 Migrations 文件夹中的文件
  3. 运行一个新的迁移命令"dnx ef migrations add "
  4. 下次尝试访问身份时,它将使用正确的 PK 类型重建表

以下是如何在 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
    }
}

如果遇到奇怪的运行时错误:

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'.

说明你忘记在AddEntityFrameworkStores<AppDbContext, int>()上添加TKey了。