entity-framework – 带有INT id列的MVC 6的ASP.Net标识

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

我得到一个错误,说可以在Role和User的ID列中插入一个null,但是如果我恢复到它的标准就可以了.

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,Int32,ApplicationUserClaim>
{
    public ApplicationUserStore() : base(new CustomsSiteDbContext()) { }
    public ApplicationUserStore(CustomsSiteDbContext context) : base(context) { }
}
public class ApplicationRoleStore : RoleStore<ApplicationRole,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

我希望这有帮助

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....