简体   繁体   English

如何使用MSBuild Task自动执行EF 4.3代码优先的SQL架构迁移脚本

[英]How to automate EF 4.3 Code-First SQL Schema Migration Script using MSBuild Task

For the background context, we are using EF 4.3.1 code-first and migrations in MVC 3 application. 对于背景上下文,我们在MVC 3应用程序中使用EF 4.3.1代码优先和迁移。 In the past, we have done dev and staging deployments manually but now we are trying to automate the process to create dev and staging build using MSBUILD Tasks and TFS as source control. 过去,我们是手动完成开发和暂存部署的,但是现在我们尝试使用MSBUILD Tasks和TFS作为源代码控制来自动化创建开发和暂存构建的过程。

My questions relates to above context as how can we generate sql migration script using EF migrations powershell commands "Update-Database -verbose -script" in MSBUILD tasks. 我的问题与以上情况有关,因为我们如何在MSBUILD任务中使用EF迁移powershell命令“ Update-Database -verbose -script”生成sql迁移脚本。

Is there any way to use native EF 4.3.1 migration commands in MSBUILD task to automate differential sql script generation ? 有什么方法可以在MSBUILD任务中使用本机EF 4.3.1迁移命令来自动生成差异sql脚本?

Thanks in advance for your inputs. 预先感谢您的输入。

You should just call powershell: See Using Powershell scripts from MSBuild, Scheduled Tasks, etc 您应该只调用powershell:请参阅从MSBuild使用Powershell脚本,计划任务等

<Target Name="AfterBuild">
    <Exec Command="powershell.exe -command &quot;&amp; {.\Register-myscript.ps1 &apos;$(TargetPath)&apos;}&quot;" />
</Target>

您可以使MSBuild Task运行Migrate.exe,如此处所述, 如何使用MSBuild和Migrate.exe运行实体框架迁移

Here is a working version of the msbuild task. 这是msbuild任务的工作版本。 It copies migrate.exe from the tools folder of EF 6 into the build directory. 它将migration.exe从EF 6的tools文件夹复制到构建目录中。 In order for this to work all the paths are absolute. 为了使它起作用,所有路径都是绝对的。

  <Target Name="AfterBuild">
<copy SourceFiles="$(SolutionDir)packages\EntityFramework.6.1.3\tools\migrate.exe" DestinationFolder="$(TargetDir)" />
<exec Command="$(TargetDir)migrate.exe *YourData.dll* Configuration /startUpDirectory:$(TargetDir) /startUpConfigurationFile:$(TargetDir)WebStart.NET.dll.config /verbose"/>
</Target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM