为什么DatabaseInitializer被调用两次? [英] Why does the DatabaseInitializer get called twice?

查看:105
本文介绍了为什么DatabaseInitializer被调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个MVC,该MVC当前使用ApplicationStart方法进行一些设置工作,因此当应用程序通过IIS应用程序池恢复工作时,该设置已经完成.

I’ve inherited an MVC that currently does some setup work with the ApplicationStart method so that when the application comes back to life with an IIS Application pool this setup has already been carried out.

作为伪代码:

    public class MvcApplication : System.Web.HttpApplication
    {
      protected void Application_Start()
      {
        // Build Api autofac container
        // Build MVC autofac container

        // Resolve serviceOne from the MVC container
        var serviceOne = (IServiceOne)DependencyResolver.Current.GetService(typeof(IServiceOne));

        // Make setup call - includes external http calls and DbContext checking
        serviceOne.syncToExternal();

        // Resolve serviceTwo again from the MVC container
        var serviceTwo = (IServiceTwo)DependencyResolver.Current.GetService(typeof(IServiceTwo));

        // Make setup call - publishes application information to internal message queues so that we know it's running
        serviceTwo.syncToInternalSystems();
      }
    }

在ApplicationStart中,我将完成设置Autofac容器的常规过程;一个用于MVC,另一个用于WebApi.在这里分别注册了MVC或Api控制器,服务类和我的DbContext.

In the ApplicationStart I run through the normal process of setting up Autofac containers; one for the MVC and one for the WebApi. In here the respective MVC or Api controllers, service classes and my DbContext are registered.

ApplicationStart中的设置工作需要一个服务和一个DbContext(我可以从MvcContainer解析),因为此时无法访问WebApi.

The setup work in the ApplicationStart needs a service and a DbContext which I resolve from the MvcContainer, as the WebApi one is not accessible at this point.

ServiceOne从外部URL检索数据,并使用它来播种/检查数据库的当前内容.

ServiceOne retrieves data from an external url and using this to seed / check the current contents of the database.

ServiceTwo读回一些数据并将其发布到公司内部的消息队列中.

ServiceTwo reads back some of this data and publishes it to internal message queues within the company.

一旦Application_Start()完成并且主页已加载:如果我请求通过MVC进行路由,则DbCobntext的注册数据库Initialzer不会像在Application_Start期间运行时那样被调用,但是如果我执行/api请求databaseInitializer确实被调用.

Once the Application_Start() has finished and the Home page has loaded: if I make a request which routes through the MVC then the DbCobntext's registered databaseInitialzer does not get called as it was run during the Application_Start, but if I make an /api request the databaseInitializer does get called.

我怀疑在ApplicationStart方法中运行安装程序会阻止在管理Connection的System.Data.Entity.Database中设置标志;因此,当从Api容器解析DbContext时,它认为数据库尚未初始化...?

I suspect that running the setup in ApplicationStart method is preventing a flag being set in the System.Data.Entity.Database which managed the Connection; hence when the DbContext is resolved from the Api Container it thinks the database hasn’t been initialised...?

任何帮助将不胜感激.

我的后退方法是将所有设置转换为种子方法,该方法在调用databaseInitialiser/Migration时运行;但是了解为什么原始代码未能按预期执行将很有用.

My fallback will be to shift all of the setup into a seed method which runs when the databaseInitialiser/Migration is called; but it would be useful to know why the original version of the code was failing to execute as expected.

推荐答案

无论何时启动ApplicationPool,ApplicationStart都会运行.您必须使用另一种机制来填充数据库.像

the ApplicationStart run every time the ApplicationPool starts, no matter what. You have to use another mechanism to populate your DB. Like Migrations.

这篇关于为什么DatabaseInitializer被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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