简体   繁体   English

如何使用 .NET 6 在 aspnet 核心 Web 应用程序中执行 database.ensurecreated()?

[英]How do you do database.ensurecreated() in aspnet core web application using .NET 6?

In a .NET 5 web application, we use code such as the following in startup.cs to initialize the DB using Entity Framework:在 .NET 5 Web 应用程序中,我们在 startup.cs 中使用如下代码来使用 Entity Framework 初始化 DB:

using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    context.Database.EnsureCreated();
}

In .NET 6 though, I get the error that there is no ApplicationServices.但是在 .NET 6 中,我收到没有 ApplicationServices 的错误。 I tried the solution proposed here , but then I get a runtime error: 'Cannot resolve scoped service 'WebApplication1.DataAccess.SchoolDbContext' from root provider'.我尝试了此处提出的解决方案,但随后出现运行时错误:'无法从根提供程序解析范围服务'WebApplication1.DataAccess.SchoolDbContext'。 The complete code in my program.cs is as follows:我的program.cs中的完整代码如下:

using WebApplication1.DataAccess;
using Microsoft.EntityFrameworkCore;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("appsettings.json");
builder.Services.AddRazorPages();

// Setup EF connection
// https://stackoverflow.com/a/43098152/1385857
// 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();

// https://stackoverflow.com/a/71258326/1385857
SchoolDbContext dbcontext = app.Services.GetRequiredService<SchoolDbContext>();
dbcontext.Database.EnsureCreated();
...

Help would be great.帮助会很棒。

WebApplication returned by builder.Build() has Services property which allows to create a scope: builder.Build()返回的WebApplication具有允许创建范围的Services属性:

using(var scope = app.Services.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<SchoolDbContext>();
    // use context
}

You can also use the async version of the CreateScope() method.您还可以使用 CreateScope() 方法的异步版本。 If you want to call an awaitable method.如果你想调用一个等待的方法。

using(var scope = app.Services.CreateAsyncScope())
{
  var dbContext = scope.ServiceProvider.GetRequiredService<SchoolDbContext>();
  //more codes ...
}

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

相关问题 Database.EnsureCreated无法正常工作 - Database.EnsureCreated not working EntityFramework Core Database.EnsureCreated不会创建数据库 - EntityFramework Core Database.EnsureCreated doesn't create database 我应该把Database.EnsureCreated放在哪里? - Where should I put Database.EnsureCreated? EF Core Database.EnsureCreated 获取创建表 SQL 并且不向数据库发送请求 - EF Core Database.EnsureCreated get creating table SQL and without sending request to database Database.EnsureCreated EF 7 ASP.NET 5 MVC 6处的堆栈溢出异常 - Stack Overflow exception at Database.EnsureCreated EF 7 ASP.NET 5 MVC 6 如何在Database.EnsureCreated()和EF Xamarin.Forms之后使用Database.Migration() - How to use Database.Migration() after Database.EnsureCreated() EF Xamarin.Forms 如何对ASP.NET核心Web应用程序(.Net Framework)进行单元测试? - How do you unit test ASP.NET Core Web Application (.Net Framework)? 如何在 ASPNET Core 3.0 中启动后台进程? - How do you launch a background process in ASPNET Core 3.0? 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 如何在ASP Net Core中授权Web API控制器 - How do you Authorize a Web API Controller in ASP Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM