具有INT id列的ASP.Net標識到MVC 6


我使用帶有Asp.net Identity的MVC6項目,並希望將ID列從字符串更改為INT。我按照這篇文章在這裡輸入鏈接描述

我得到一個錯誤,說可以在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, 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

我希望這有幫助


熱門答案

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

如果你得到奇怪的運行時錯誤:

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

這意味著您忘記在AddEntityFrameworkStores<AppDbContext, int>()上添加TKey。





許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因