ASP.NET:检查是否从迁移运行 [英] ASP.NET: Check if run from migration
本文介绍了ASP.NET:检查是否从迁移运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 ConfigureServices
中有一些代码在运行迁移时失败:
I have some code in my ConfigureServices
that fails when running a migration:
dotnet ef migrations list
我正在尝试添加证书,但是找不到文件(在整体启动项目时可以使用).有没有办法做这样的事情:
I'm trying to add a Certificate but it can't find the file (it works when starting the project as a whole). So is there a way to do something like this:
if (!CurrentEnvironment.IsMigration()) {
doMyStuffThatFailsInMigration()
}
这样,我可以保留我的代码,但是在不运行迁移时仅执行它即可.
That way I could keep my code as it is but just execute it when not running it in a migration.
谢谢
推荐答案
只需在Main方法中设置一个静态标志(dotnet-ef工具不会调用):
Just set a static flag in the Main method (which is not called by the dotnet-ef tool):
public class Program
{
public static bool IsStartedWithMain { get; private set; }
public static void Main(string[] args)
{
IsStartedWithMain = true;
...
}
}
,然后在需要时进行检查:
and then check it when needed:
internal static void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
if (Program.IsStartedWithMain)
{
// do stuff which must not be run upon using the dotnet-ef tool
}
}
这篇关于ASP.NET:检查是否从迁移运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文