EntityFrameworkCore与Mysql
我正在使用Mysql的身份和实体框架。当我运行命令时EntityFrameworkCore与Mysql,mysql,entity-framework,Mysql,Entity Framework,我正在使用Mysql的身份和实体框架。当我运行命令时 dotnet ef database update 我得到以下错误 实体类型“HistoryRow”需要定义主键 上下文文件 public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<Applic
dotnet ef database update
我得到以下错误
实体类型“HistoryRow”需要定义主键
上下文文件
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 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);
modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
// modelBuilder.Entity<HistoryRow>().Property(h => h.ProductVersion).HasMaxLength(100).IsRequired();
modelBuilder.Entity<ApplicationUser>(entity => entity.Property(m => m.Id).HasMaxLength(127));
modelBuilder.Entity<IdentityRole>(entity => entity.Property(m => m.Id).HasMaxLength(127));
modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.Property(m => m.LoginProvider).HasMaxLength(127);
entity.Property(m => m.ProviderKey).HasMaxLength(127);
});
modelBuilder.Entity<IdentityUserRole<string>>(entity =>
{
entity.Property(m => m.UserId).HasMaxLength(127);
entity.Property(m => m.RoleId).HasMaxLength(127);
});
modelBuilder.Entity<IdentityUserToken<string>>(entity =>
{
entity.Property(m => m.UserId).HasMaxLength(127);
entity.Property(m => m.LoginProvider).HasMaxLength(127);
entity.Property(m => m.Name).HasMaxLength(127);
});
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
//自定义ASP.NET标识模型,并根据需要覆盖默认值。
//例如,可以重命名ASP.NET标识表名称等。
//在调用base.OnModelCreating(builder)后添加自定义项;
modelBuilder.Entity().Property(h=>h.MigrationId).HasMaxLength(100).IsRequired();
//modelBuilder.Entity().Property(h=>h.ProductVersion).HasMaxLength(100).IsRequired();
modelBuilder.Entity(Entity=>Entity.Property(m=>m.Id).HasMaxLength(127));
modelBuilder.Entity(Entity=>Entity.Property(m=>m.Id).HasMaxLength(127));
modelBuilder.Entity(Entity=>
{
Property(m=>m.LoginProvider).HasMaxLength(127);
Property(m=>m.ProviderKey).HasMaxLength(127);
});
modelBuilder.Entity(Entity=>
{
Property(m=>m.UserId).HasMaxLength(127);
Property(m=>m.RoleId).HasMaxLength(127);
});
modelBuilder.Entity(Entity=>
{
Property(m=>m.UserId).HasMaxLength(127);
Property(m=>m.LoginProvider).HasMaxLength(127);
Property(m=>m.Name).HasMaxLength(127);
});
}
}
如果我通过fluent API将迁移ID设置为主键
modelBuilder.Entity<HistoryRow>().HasKey(h => h.MigrationId)
modelBuilder.Entity().HasKey(h=>h.MigrationId)
然后它会给出类似于
无法将主键设置为“HistoryRow”,因为它不可访问
StartUp.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<ApplicationDbContext>(options => options.UseMySQL("Server=localhost;port=3306;database=netcore;uid=root;Password=password"));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryScopes(Config.GetScopes())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
}
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddApplicationInsightsTelemetry(配置);
services.AddDbContext(options=>options.UseMySQL(“Server=localhost;port=3306;database=netcore;uid=root;Password=Password”);
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
//添加应用程序服务。
services.AddTransient();
services.AddTransient();
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryScopes(Config.GetScopes())
.AddInMemoryClients(Config.GetClients())
.addAsNetIdentity();
}
我正在使用SapientGuarden.EntityFrameWork.MySql包