问题描述
我无法针对我创建的基本.NET Core 2.2 Web API运行EntityFramework Core命令。该API正常运行,但除非更改连接字符串的检索位置,否则无法进行添加迁移或更新数据库。我相信第一个示例是最佳实践,因为连接字符串更安全,但是在尝试运行EF Core命令时出现错误。
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.
在传递DbContextOptions<>时,我已正确使用AddDbContext()。唯一的代码差异是Startup.ConfigureServices()和MyContext.OnConfiguring()。 我的首选示例在做什么?
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?
首选示例(EF命令无效)
// 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();
}
}
使用以下代码,我可以运行'add -migration和 update-database没有错误,但是我认为这种方式检索连接字符串的安全性较低。
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.
示例2(EF命令工作)
// 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();
}
}
推荐答案
解决了对我来说更新到.NET Core 3.1.101和EFCore 3.1.1的问题。
What fixed the issue for me was updating to .NET Core 3.1.101 and EFCore 3.1.1.
这篇关于无法运行命令。错误:尚未为此DbContext配置数据库提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!