我正在使用Mysql使用Identity和Entity Framework。当我运行命令
时 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);
});
}
}
如果我通过流畅的API将 MigrationId 设置为主键
modelBuilder.Entity<HistoryRow>().HasKey(h => h.MigrationId)
然后它会出现类似
的错误您无法将主键设置为&#39; HistoryRow&#39;因为它无法访问
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>();
}
我正在使用SapientGuarden.EntityFrameWork.MySql包