[英]EF Core Add-Migration fails with System.MissingMethodException: No parameterless constructor defined
I am using Ef Core 3.1 and I have the following simple DBContext.我正在使用 Ef Core 3.1 并且我有以下简单的 DBContext。
public class UserDbContext : DbContext
{
public UserDbContext (DbContextOptions<UserDbContext> options) : base(options)
{
}
public DbSet<User> Users{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
And here is my Startup:这是我的启动:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<Settings>(Configuration);
var appSettings = services.BuildServiceProvider().GetService<IOptions<Settings>>().Value;
services.AddDbContext<DbContext>(options =>
options.UseSqlServer(appSettings.DataStoreSettings.ConnectionString));
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<DbContext>();
context.Database.Migrate();
}
}
}
When starting my website, the database gets created successfully with only dbo.__EFMigrationsHistory table.启动我的网站时,仅使用 dbo.__EFMigrationsHistory 表成功创建了数据库。
When I try to add a migration using:当我尝试使用以下方法添加迁移时:
Add-Migration Initial -c UserDbContext -Verbose
I get the following Error:我收到以下错误:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'UserDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException:无法创建类型为“UserDbContext”的对象。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for type 'UserDbContext'.
有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for type 'UserDbContext'。
Adding a parameterless constructor to my context results:将无参数构造函数添加到我的上下文结果中:
No database provider has been configured for this DbContext.
尚未为此 DbContext 配置数据库提供程序。 A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider.
可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。 If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
如果使用 AddDbContext,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。
I am not sure why Add-Migration
is not working, can't I use Dependency Injection with EF core, if I need to use migrations?我不确定为什么
Add-Migration
不起作用,如果我需要使用迁移,我不能对 EF 核心使用依赖注入吗?
The issue was in registering DbContext
in IOC.问题
DbContext
在 IOC 中注册DbContext
。
Replacing:更换:
services.AddDbContext<DbContext>(options =>
options.UseSqlServer(appSettings.DataStoreSettings.ConnectionString));
With:和:
services.AddDbContext<UserDbContext>(options =>
options.UseSqlServer(appSettings.DataStoreSettings.ConnectionString));
Fixed it.修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.