有没有办法从已发布的DLL运行EF Core RC2工具? [英] Is there a way to run EF Core RC2 tools from published DLL?

查看:100
本文介绍了有没有办法从已发布的DLL运行EF Core RC2工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布.Net Core RC1应用程序后,在project.json中指定的命令为它们创建了相应的.cmd文件,可以在部署后执行这些文件(例如,web.cmd和ef.cmd).就我而言,我想在我的部署目标上运行以下Entity Framework命令:

After publishing a .Net Core RC1 application, commands specified in the project.json had corresponding .cmd files created for them which could be executed after deployment (e.g. web.cmd and ef.cmd). In my case, I want to run the following Entity Framework command on my deployment target:

dotnet ef database update -c MyContext

当我从包含源代码的文件夹中运行此文件时,它运行良好,但是在发布后,它似乎没有在已编译的DLL中找到命令.我对RC2命令更改的理解是,工具"可以作为名为dotnet-*.dll的独立应用程序进行编译,并且可以通过CLI执行.如何在发布的输出中将Entity Framework Core工具公开为可执行DLL?

This works fine when I run this from the folder containing the source code, however after publishing, it doesn't appear to find the command within the compiled DLLs. My understanding of the change in commands with RC2 is that 'tools' can be compiled as standalone applications named dotnet-*.dll and can be executed via the CLI. How can the Entity Framework Core tools be exposed as executable DLLs in the published output?

仅供参考,我的构建/部署工作流程如下:

FYI, my build/deployment workflow is as follows:

TeamCity

dotnet恢复=> dotnet构建=> dotnet测试=> dotnet发布

dotnet restore => dotnet build => dotnet test => dotnet publish

章鱼部署

上传软件包=> EF更新数据库=>等等

Upload Package => EF Update Database => etc

推荐答案

我在一个项目中遇到了同样的问题,但是由于多种原因,我不希望迁移在应用程序启动时自动运行.

I ended up in the same problem on a project but for several reasons I don't want migrations to run automatically on application boot.

为解决此问题,我更新了Program.cs以使用两个参数(下面列出了完整代码)

To solve it I updated Program.cs to take two arguments (full code is listed below)

  • --ef-migrate,以应用所有待处理的迁移,并且
  • --ef-migrate-check,以验证是否已应用所有迁移
  • --ef-migrate, to apply all pending migrations, and
  • --ef-migrate-check, to validate if all migrations have been applied

如果存在参数,则将执行EF操作并退出程序,否则将启动Web应用程序.

If arguments are present then the EF actions are applied and the program exits, otherwise the web application is launched.

请注意,它依赖于Microsoft.Extensions.CommandLineUtils包来简化命令行解析.

Please note that it depends on the Microsoft.Extensions.CommandLineUtils package to ease the command line parsing.

对于octopus部署,然后可以将软件包两次发布到不同的位置-一个用于运行迁移,另一个用于虚拟主机.在我们的例子中,我们添加了一个后部署PowerShell脚本",其中包含内容

For octopus deploy one can then publish the package twice to seperate locations - one for running migrations and the other for webhosting. In our case, we added a "post deploy powershell script" with the content

$env:ASPNETCORE_ENVIRONMENT="#{Octopus.Environment.Name}"
dotnet example-app.dll --ef-migrate

在Docker上下文中,它也可以完美运行

docker run -it "example-app-container" dotnet example-app.dll --ef-migrate

完整的Program.cs,不包括名称空间和用法:

Full Program.cs excluding namespace and usings:

//Remember to run: dotnet add package Microsoft.Extensions.CommandLineUtils
public class Program
{
    public static void Main(string[] args)
    {
        var commandLineApplication = new CommandLineApplication(false);
        var doMigrate = commandLineApplication.Option(
            "--ef-migrate",
            "Apply entity framework migrations and exit",
            CommandOptionType.NoValue);
        var verifyMigrate = commandLineApplication.Option(
            "--ef-migrate-check",
            "Check the status of entity framework migrations",
            CommandOptionType.NoValue);
        commandLineApplication.HelpOption("-? | -h | --help");
        commandLineApplication.OnExecute(() =>
        {
            ExecuteApp(args, doMigrate, verifyMigrate);
            return 0;
        });
        commandLineApplication.Execute(args);
    }

    private static void ExecuteApp(string[] args, CommandOption doMigrate, CommandOption verifyMigrate)
    {
        Console.WriteLine("Loading web host");
        var webHost = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        if (verifyMigrate.HasValue() && doMigrate.HasValue())
        {
            Console.WriteLine("ef-migrate and ef-migrate-check are mutually exclusive, select one, and try again");
            Environment.Exit(2);
        }

        if (verifyMigrate.HasValue())
        {
            Console.WriteLine("Validating status of Entity Framework migrations");
            using (var context = webHost.Services.GetService<DatabaseContext>())
            {
                var pendingMigrations = context.Database.GetPendingMigrations();
                var migrations = pendingMigrations as IList<string> ?? pendingMigrations.ToList();
                if (!migrations.Any())
                {
                    Console.WriteLine("No pending migratons");
                    Environment.Exit(0);
                }

                Console.WriteLine("Pending migratons {0}", migrations.Count());
                foreach (var migration in migrations)
                {
                    Console.WriteLine($"\t{migration}");
                }

                Environment.Exit(3);
            }
        }

        if (doMigrate.HasValue())
        {
            Console.WriteLine("Applyting Entity Framework migrations");
            using (var context = webHost.Services.GetService<DatabaseContext>())
            {
                context.Database.Migrate();
                Console.WriteLine("All done, closing app");
                Environment.Exit(0);
            }
        }

        // no flags provided, so just run the webhost
        webHost.Run();
    }
}

这篇关于有没有办法从已发布的DLL运行EF Core RC2工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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