我无法对我创建的基本.NET Core 2.2 Web API 运行 EntityFramework Core 命令。API正在工作,但是如果我不更改连接字符串的检索位置,我将无法执行“add-migration”或“update-database”。我认为第一个示例是最佳实践,因为连接字符串更安全,但是当我尝试运行EF Core命令时会出现错误。
引用:"此DbContext尚未配置数据库提供程序。可以通过覆盖DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基础构造函数。"
据我所知,我已正确使用了 AddDbContext() 并传递了 DbContextOptions<>。 在 Startup.ConfigureServices() 和 MyContext.OnConfiguring() 中唯一的代码差异在于什么?我的首选示例有什么问题吗?
使用以下代码,我能够成功运行'add-migration'和'update-database',但是我认为这种方式检索连接字符串的安全性较低。 示例2(EF命令工作)
引用:"此DbContext尚未配置数据库提供程序。可以通过覆盖DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基础构造函数。"
据我所知,我已正确使用了 AddDbContext() 并传递了 DbContextOptions<>。 在 Startup.ConfigureServices() 和 MyContext.OnConfiguring() 中唯一的代码差异在于什么?我的首选示例有什么问题吗?
// 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',但是我认为这种方式检索连接字符串的安全性较低。 示例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();
}
}