.NET CORE2.0 内置Identity 迁移至Mysql主键过长的解决方案
使用EntityFrameworkMysql+NETCORE2.0内置Identity时,会报错主键过长导致三张核心表无法创建,在ApplicationDbContext中重写OnModelCreating即可
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //和设置的表主键对应 builder.Entity<IdentityUserLogin<int>>(e => e.Property(p => p.LoginProvider).HasMaxLength(128)); builder.Entity<IdentityUserLogin<int>>(e => e.Property(p => p.ProviderKey).HasMaxLength(128)); builder.Entity<IdentityUserToken<int>>(e => e.Property(p => p.LoginProvider).HasMaxLength(128)); builder.Entity<IdentityUserToken<int>>(e => e.Property(p => p.Name).HasMaxLength(128)); } }
重新执行迁移数据库代码,可以正常生成了!