Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET:检查是否从迁移运行_C#_Asp.net_Entity Framework_Asp.net Core_Entity Framework Migrations - Fatal编程技术网

C# ASP.NET:检查是否从迁移运行

C# ASP.NET:检查是否从迁移运行,c#,asp.net,entity-framework,asp.net-core,entity-framework-migrations,C#,Asp.net,Entity Framework,Asp.net Core,Entity Framework Migrations,我的ConfigureServices中有一些代码在运行迁移时失败: dotnet ef migrations list 我正在尝试添加一个证书,但它找不到该文件(它在整个项目启动时起作用)。那么有没有办法做到这一点: if (!CurrentEnvironment.IsMigration()) { doMyStuffThatFailsInMigration() } 这样,我可以保持代码的原样,但在迁移中不运行代码时只执行它 感谢我当前用于检测是否未发生迁移的解决方案: using

我的
ConfigureServices
中有一些代码在运行迁移时失败:

dotnet ef migrations list
我正在尝试添加一个证书,但它找不到该文件(它在整个项目启动时起作用)。那么有没有办法做到这一点:

if (!CurrentEnvironment.IsMigration()) {
    doMyStuffThatFailsInMigration()
}
这样,我可以保持代码的原样,但在迁移中不运行代码时只执行它


感谢

我当前用于检测是否未发生迁移的解决方案:

using System.Linq;
// app is of type IApplicationBuilder
// RegisteredDBContext is the DBContext I have dependency injected
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<RegisteredDBContext>();
            if (context.Database.GetPendingMigrations().Any())
            {
                var msg = "There are pending migrations application will not start. Make sure migrations are ran.";
                throw new InvalidProgramException(msg);
                // Instead of error throwing, other code could happen
            }
        }
使用System.Linq;
//应用程序的类型为IAApplicationBuilder
//RegisteredDBContext是我已注入依赖项的DBContext
使用(var serviceScope=app.ApplicationServices.GetRequiredService().CreateScope())
{
var context=serviceScope.ServiceProvider.GetService();
if(context.Database.GetPendingMigrations().Any())
{
var msg=“存在挂起的迁移应用程序将不会启动。请确保迁移已运行。”;
抛出新的InvalidProgrameException(msg);
//除了抛出错误,还可能发生其他代码
}
}
这假设迁移已经同步到数据库。如果只调用了
EnsureDatabase
,则此方法不起作用,因为迁移仍处于挂起状态


context.Database
上还有其他方法选项
GetMigrations
getappliedmiglations

只需在Main方法中设置一个静态标志(dotnet ef工具不会调用该标志):

然后在需要时进行检查:

internal static void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
    if (Program.IsStartedWithMain)
    {
        // do stuff which must not be run upon using the dotnet-ef tool
    }
}

问题不在于是否发生了迁移。这与运行时环境有关,即当前进程是正常托管还是应用迁移的dotnet ef工具。
internal static void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
    if (Program.IsStartedWithMain)
    {
        // do stuff which must not be run upon using the dotnet-ef tool
    }
}