尝试执行迁移时出现此错误
System.MissingMethodException:未定义无参数构造函数 对于这个对象。在 System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔 publicOnly,布尔值wrapExceptions,布尔值和canBeCached, RuntimeMethodHandleInternal&ctor)
我想做的是在dbcontext类中使用Iconfiguration。那有可能吗?这是我目前的操作方式:
public class AppDbContext : DbContext
{
public IConfiguration Configuration { get; }
public AppDbContext(DbContextOptions<AppDbContext> options,) :
base(options) {
Configuration = configuration;
}
public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(schema: Configuration["DatabaseConfiguration:SchemaName"]);
}
}
答案 0 :(得分:0)
请按照以下步骤实现您的要求:
修改AppDbContext
public class AppDbContext : DbContext
{
public IConfiguration Configuration { get; }
public AppDbContext(DbContextOptions<AppDbContext> options, IConfiguration configuration) :
base(options) {
Configuration = configuration;
}
public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema(schema: Configuration["DatabaseConfiguration:SchemaName"]);
}
}
从Package Manager Console
运行add-migration init
从Package Manager Console
运行update-database