我正在使用带有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; }
}
答案 0 :(得分:3)
以下是如何在Identity上使用整数列,Asp NET Core&amp;实体框架核心:
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。
答案 1 :(得分:1)
我之前为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
我希望这会有所帮助
答案 2 :(得分:1)
我发现了这个问题。它与迁移有关。当VS2015首次从模板创建项目时,它已经作为迁移构建,并将列作为字符串,即使我在原始帖子中将代码更改为INT。所以你需要做的是以下