【问题标题】:Detect EF core 2.0 tooling from Startup.cs从 Startup.cs 检测 EF core 2.0 工具
【发布时间】:2017-10-19 13:00:23
【问题描述】:

我的 MVC 核心 2.0 项目的 Startup.cs 中有一些代码,我只想在应用实际启动时运行。

有没有办法检测应用程序何时从 EF 工具启动?

【问题讨论】:

    标签: .net entity-framework asp.net-core-mvc


    【解决方案1】:

    事实证明,使用 DotNet MVC Core 2.0 的答案在于将不应由工具运行的任何代码放入 program.cs main,因为 BuildWebHost 是唯一由工具执行的代码。

        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);
    
            // Code placed here will not be executed by EF tooling as it calls BuildWebHost directly
    
            host.Run();
        }
    
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2019-03-13
      • 2018-04-30
      相关资源
      最近更新 更多