4Manuals

  • PDF Cloud HOME

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

    获取Hough线的交点OpenCV C ++ 如何将字符串“ text1”设置为包含项目text1,text2,test3的组合框的选定项目? 为什么System.Net.Mail.MailAddress构造函数在域部分中解析带有斜杠“ /”的电子邮件? Android 10,API29:在应用程序文件夹中使用C ++库编写文件会使应用程序崩溃 Unity-LineRenderer没有出现在版本(2D)上 按下按钮后,电话中未显示通知 ASP.net,使用WebConfig httpErrors重定向到区域内的控制器不起作用 获取会议室详细信息 我们如何将参数Line转换为Station? 网络核心:查找实体框架核心的主键和反射

我正在使用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)
    {
    }
}

这是我的创业公司:

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();
        }
    }
}

Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

启动我的网站时,仅使用dbo .__ EFMigrationsHistory表即可成功创建数据库。

当我尝试使用以下方式添加迁移时:

Add-Migration Initial -c UserDbContext -Verbose

我收到以下错误消息:

  

Microsoft.EntityFrameworkCore.Design.OperationException:无法执行   创建一个类型为“ UserDbContext”的对象。对于不同的模式   在设计时受支持,请参阅   https://go.microsoft.com/fwlink/?linkid=851728 --->   System.MissingMethodException:未定义无参数构造函数   用于类型'UserDbContext'。

在我的上下文结果中添加无参数构造函数:

  

尚未为此DbContext配置数据库提供程序。一种   可以通过重写DbContext.OnConfiguring来配置提供程序   方法或通过在应用程序服务提供程序上使用AddDbContext。   如果使用AddDbContext,则还要确保您的DbContext类型   在其构造函数中接受DbContextOptions对象,并   将其传递给DbContext的基本构造函数。

我不确定为什么Add-Migration无法正常工作,如果我需要使用迁移,是否不能将依赖注入与EF核心一起使用?

1 个答案:

答案 0 :(得分:1)

问题在于在IOC中注册DbContext。

替换:

services.AddDbContext<DbContext>(options => 
            options.UseSqlServer(appSettings.DataStoreSettings.ConnectionString));

使用:

services.AddDbContext<UserDbContext>(options => 
            options.UseSqlServer(appSettings.DataStoreSettings.ConnectionString));

解决了。



Similar searches
    具有for循环的单行bash函数 Google搜索结果的事件监听器 模拟org.springframework.web.reactive.function.client.WebClient.ResponseSpec#onStatus输入参数 addEventListener没有响应以在反应中实现芯片 Samsung SR-L67 Refrigerator User Manual