简体   繁体   English

EF Core Add-Migration 因 System.MissingMethodException 失败:未定义无参数构造函数

[英]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.

相关问题 Ef Core: Add-Migration Fail No parameterless constructor defined 错误 - Ef Core : Add-Migration Fail No parameterless constructor defined error System.MissingMethodException:没有为此对象定义无参数构造函数 - System.MissingMethodException: No parameterless constructor defined for this object ASP Net Core MVC创建实例控制器System.MissingMethodException:&#39;为此对象未定义无参数构造函数。 - asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.' System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 未处理的异常。 System.MissingMethodException:没有为类型定义无参数构造函数 - Unhandled exception. System.MissingMethodException: No parameterless constructor defined for type System.MissingMethodException:在mvc4中没有为此对象定义无参数构造函数 - System.MissingMethodException: No parameterless constructor defined for this object in mvc4 错误 System.MissingMethodException:没有为“System.Linq.Expressions.Expression”类型定义无参数构造函数 - Error System.MissingMethodException: No parameterless constructor defined for type of 'System.Linq.Expressions.Expression`1 EF System.MissingMethodException - EF System.MissingMethodException EF Core 1.1到WebApi核心 - 添加迁移失败 - EF Core 1.1 to WebApi Core - Add-Migration fails System.MissingMethodException - 构造函数中的 Setter - System.MissingMethodException - Setter in Constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM