我有一个ASP.NET MVC 6应用程序,我需要调用Database.EnsureCreated和Database.Migrate方法.
但我应该在哪里打电话给他们?
在.NET 5 Web应用程序中,我们在startup.cs中使用如下代码来使用实体框架初始化数据库:
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.EnsureCreated();
}
Run Code Online (Sandbox Code Playgroud)
但在 .NET 6 中,我收到没有 ApplicationServices 的错误。我尝试了此处提出的解决方案,但随后出现运行时错误:“无法从根提供程序解析作用域服务‘WebApplication1.DataAccess.SchoolDbContext’”。我的program.cs中的完整代码如下:
using WebApplication1.DataAccess;
using Microsoft.EntityFrameworkCore;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json");
builder.Services.AddRazorPages();
// Setup EF connection
// /sf/answers/3016870671/
// https://medium.com/executeautomation/asp-net-core-6-0-minimal-api-with-entity-framework-core-69d0c13ba9ab
builder.Services.AddDbContext<SchoolDbContext>(options =>
options.UseSqlServer(builder.Configuration["Data:SchoolLocal:ConnectionString"]));
WebApplication app = builder.Build();
// /sf/answers/4988082851/
SchoolDbContext dbcontext = app.Services.GetRequiredService<SchoolDbContext>();
dbcontext.Database.EnsureCreated();
...
Run Code Online (Sandbox Code Playgroud)
帮助会很棒。
我试图Microsoft.EntityFrameworkCore.SqlServer通过运行命令添加命名的包.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Run Code Online (Sandbox Code Playgroud)
在Visual Studio代码中,但我收到此错误:
软件包Microsoft.EntityFrameworkCore.SqlServer 2.0.0与netcoreapp1.1(.NETCoreApp,Version = v1.1)不兼容.软件包Microsoft.EntityFrameworkCore.SqlServer 2.0.0支持:netstandard2.0"(.NETStandard,Version = v2.0).软件包'Microsoft.EntityFrameworkCore.SqlServer'与项目'C:\ users\username中的'all'框架不兼容.."
我IAsyncQueryProvider在使用EntityFrameworkCore时无法注入自定义.更准确地说,在使用提供的内存数据库功能时,我无法注入提供程序.使用默认提供程序(SqlServer),一切正常.
这是我的全球 Startup.cs
private void ConfigureEntityFrameworkWithSecurity(IServiceCollection services)
{
services
.AddEntityFramework()
.AddEntityFrameworkSqlServer()
.AddScoped<IAsyncQueryProvider, CustomEntityProvider>()
.AddDbContext<APIContext>((sp, options) =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
.UseInternalServiceProvider(sp);
});
}
Run Code Online (Sandbox Code Playgroud)
这完美无缺,我可以在其中设置一个断点CustomEntityProvider来验证它是否确实被注入.目前,CustomEntityProvider只需实现IAsyncQueryProvider,并简单地通过请求.其中没有包含逻辑.
当我运行测试时,我将webhost配置为使用不同的Startup文件:
public class TestStartup : Startup
{
public TestStartup(IHostingEnvironment env) : base(env)
{
}
public override void ConfigureServices(IServiceCollection services)
{
services
.AddDbContext<APIContext>((sp, options) =>
{
options.UseInMemoryDatabase()
.UseInternalServiceProvider(sp);
});
base.ConfigureServices(services);
}
}
Run Code Online (Sandbox Code Playgroud)
运行测试会TestStartup产生错误:
System.InvalidOperationException:尚未为此DbContext配置数据库提供程序.可以通过覆盖DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序.如果使用AddDbContext,那么还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基础构造函数.
并且APIContext正确定义:
public class APIContext : DbContext
{
public APIContext(DbContextOptions<APIContext> …Run Code Online (Sandbox Code Playgroud)