问题
I am unable to run EntityFramework Core commands against the basic .NET Core 2.2 Web API I created. The API is working, but I can not 'add-migration' or 'update-database' unless I change where the connection string is retrieved. I believe the first example is best practice because the connection string is more secure, but I get an error when trying to run EF Core commands.
"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. 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."
As far as I can tell I have correctly used AddDbContext() while passing DbContextOptions<>. The only code differences are in Startup.ConfigureServices() and MyContext.OnConfiguring(). What am I doing wrong with my preferred example?
Preferred Example (EF Commands do not work)
// MyContext.cs
public class MyContext : DbContext
{
private readonly DbContextOptions<MyContext> _options;
private readonly IConfiguration _config;
public MyContext (DbContextOptions<MyContext> options, IConfiguration config) : base(options)
{
_options = options;
_config = config;
}
public DbSet<Account> Accounts { get; set; }
public DbSet<User> User { get; set; }
}
}
// Startup.cs
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("MyAPI")));
services.AddScoped<IMyRepository, MyRepository>();
services.AddAutoMapper();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
else { app.UseHsts(); }
//app.UseHttpsRedirection();
app.UseMvc();
}
}
With the code below I am able to run 'add-migration' and 'update-database' with no errors, but I believe the retrieval of the connection string is less secure this way.
Example 2 (EF Commands Work)
// MyContext.cs
public class MyContext : DbContext
{
private readonly DbContextOptions<MyContext> _options;
private readonly IConfiguration _config;
public MyContext (DbContextOptions<MyContext> options, IConfiguration config) : base(options)
{
_options = options;
_config = config;
}
public DbSet<Account> Accounts { get; set; }
public DbSet<User> User { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_config.GetConnectionString("MyAPI"));
}
}
}
// Startup.cs
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>();
services.AddScoped<IMyRepository, MyRepository>();
services.AddAutoMapper();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
else { app.UseHsts(); }
//app.UseHttpsRedirection();
app.UseMvc();
}
}
回答1:
What fixed the issue for me was updating to .NET Core 3.1.101 and EFCore 3.1.1.
回答2:
You just need to specify where the DB connection is being created.
dotnet ef migrations add SomeMigration --startup-project ../path/to/Api/project
In this example, you are running the migration command and specifying the path to the project which contains the configuration/set up of the database context. If you don't specify this, and the setup is not in the DbContext class, then EF doesn't know how the database should be configured.
来源:https://stackoverflow.com/questions/55143273/unable-to-run-commands-error-no-database-provider-has-been-configured-for-this