简体   繁体   English

没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序

[英]No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring

I have this database configuration to at my Startup class in .NET Core 3.1 Web Api project.我在 .NET Core 3.1 Web Z72664DC0959F3B0C043891A 项目中的Startup class 中有此数据库配置。

  public void ConfigureServices(IServiceCollection services)
  {
        var abcConnString = Configuration.GetConnectionString("abcConnString");
        services.RegisterAbcSqlServer<ABCContext>(abcConnString);

        var xyzConnString = Configuration.GetConnectionString("xyzConnString");
        services.RegisterXyzSqlServer<XYZContext>(xyzConnString);

        // code removed for brevity.
  }

and

public static class RegisterDbContext
{        
    public static void RegisterAbcSqlServer<T>(this IServiceCollection services, string connectionString)
        where T : DbContext, IAbcContext
    {
        var migrationsAssembly = typeof(RegisterDbContext).GetTypeInfo().Assembly.GetName().Name;
        services.AddDbContext<T>(options => options.UseSqlServer(connectionString, 
            sql => sql.MigrationsAssembly(migrationsAssembly))); 
    }

    public static void RegisterXyzSqlServer<T>(this IServiceCollection services, string connectionString)
                where T : DbContext, IXyzContext
    {
        var migrationsAssembly = typeof(RegisterDbContext).GetTypeInfo().Assembly.GetName().Name;
        services.AddDbContext<T>(options => options.UseSqlServer(connectionString,
            sql => sql.MigrationsAssembly(migrationsAssembly)));
    }
}

I can build without error.我可以毫无错误地构建。 But when I perform EF Core add-migration, it hits error:但是当我执行 EF Core 添加迁移时,它会遇到错误:

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 object 并将其传递给 DbContext 的基本构造函数。

You can get this error when there is no way for the DI framework to configure the provider by injecting a DbContextOptions .当 DI 框架无法通过注入DbContextOptions来配置提供程序时,您可能会收到此错误。 So you just need to add a constructor that will accept one, or add a parameter for a DbContextOptions to be passed in. For example:所以你只需要添加一个可以接受的构造函数,或者为要传入的DbContextOptions添加一个参数。例如:

public class XYZContext : DbContext
{
    // Add this constructor
    public XYZContext (DbContextOptions options) : base(options)
    {
    }  

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以通过覆盖DbContext.OnConfiguring来配置提供程序 - A provider can be configured by overriding the DbContext.OnConfiguring InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序。 - InvalidOperationException: No database provider has been configured for this DbContext. 没有为此 DbContext &gt; 配置数据库提供程序。 为身份配置 SQL Lite - No database provider has been > configured for this DbContext. Configuring SQL Lite for Identity ASP.NET核心InvalidOperationException:&#39;尚未为此DbContext配置数据库提供程序。 - ASP.NET core InvalidOperationException: 'No database provider has been configured for this DbContext./ 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” ASP.NET API:尚未为此DbContext配置任何数据库提供程序 - ASP.NET API: No database provider has been configured for this DbContext add-Migration 错误 没有为此 DbContext 配置数据库提供程序 - add-Migration Error No database provider has been configured for this DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM