我应该在哪里放置Database.EnsureCreated? [英] Where should I put Database.EnsureCreated?

查看:410
本文介绍了我应该在哪里放置Database.EnsureCreated?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Entity Framework Core + ASP.NET Core应用程序,当我的应用程序启动时,我想确保已创建数据库,并且最终(一旦进行迁移)我想确保它们也可以运行。 / p>

最初,我将 Database.EnsureCreated()放入我的 DbContext ,但似乎每次有人点击我的应用程序都将运行,因为每次都会创建 DbContext 的新实例。



我试图将其放入启动代码中,但是我需要一个 DbContext 的实例来执行此操作,目前尚不清楚如何确切地获得它。我正在这样配置EF:

  serviceCollection.AddEntityFramework()
.AddSqlServer()
.AddDbContext< ; Models.MyContext>(options => options.UseSqlServer(...));

我看不到从服务集合中获取DbContext实例的方法,没有看到任何合适的单例将DbContext注入其中,因此我可以进行一些一次性初始化。



那么确保与我的代码相关的最佳位置是什么?

解决方案

在编写本文时,没有正确的运行位置应用程序启动时的代码,使其可以在请求范围内执行(请参见 https://github.com/aspnet / Hosting / issues / 373 )。



目前,解决方法是执行以下操作,但不适用于更复杂的多应用程序方案(请参见 https://github.com/aspnet/EntityFramework/issues/3070# issuecomment-142752126

  public class Startup 
{
...

public void Configure(IApplicationBuilder applicationBuilder,...)
{
...
// //注意:必须在配置
的末尾进行。var serviceScopeFactory = applicationBuilder.ApplicationServices.GetRequiredService< IServiceScopeFactory>()
使用(var serviceScope = serviceScopeFactory.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService< MyDbContext>();
dbContext.Database.EnsureCreated();
}
}
}


I have an Entity Framework Core + ASP.NET Core application and when my application starts up I want to ensure that the database is created, and eventually (once I have migrations) I want to ensure that those are also run.

Initially I put Database.EnsureCreated() into the constructor of my DbContext but that appears to run every time someone hits my application since a new instance of the DbContext is created each time.

I tried to put it into my startup code, but I need an instance of my DbContext to do that and it is unclear how exactly to get one. I am configuring EF as so:

serviceCollection.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<Models.MyContext>(options => options.UseSqlServer(...));

I don't see a way to get an instance of the DbContext from the service collection, and I don't see any appropriate singleton to inject a DbContext into so I can do some one-time initialization.

So what is the best place to ensure some code related to my DbContext is called once per application run?

解决方案

At the time of this writing, there is not a "correct" place to run code on application startup such that it executes within the request scope (see https://github.com/aspnet/Hosting/issues/373).

For now, the workaround is to do the following, but it won't work in more complex multi-application scenarios (see https://github.com/aspnet/EntityFramework/issues/3070#issuecomment-142752126)

public class Startup
{
    ...

    public void Configure(IApplicationBuilder applicationBuilder, ...)
    {
        ...
        // NOTE: this must go at the end of Configure
        var serviceScopeFactory = applicationBuilder.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
        using (var serviceScope = serviceScopeFactory.CreateScope())
        {
            var dbContext = serviceScope.ServiceProvider.GetService<MyDbContext>();
            dbContext.Database.EnsureCreated();
        }
    }
}

这篇关于我应该在哪里放置Database.EnsureCreated?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆