以编程方式创建代码优先迁移 [英] Programmatically creating code first migrations
问题描述
我在一个项目中,我们在数据库上使用Entity Framework上的Code First。
I am on a project where we are using Code First on Entity Framework for our database.
我们想更改所有持续集成,以消耗生成的MSI
We want to change all of our continuous integration to consume a generated MSI package downstream, but with EF that presents a few complications.
- 当模型更改时,我们必须生成基于代码的迁移,否则该软件包将中断(数据库与模型)
- 我们更希望从团队中删除迁移的创建(基于https://msdn.microsoft.com/zh-cn/data/dn481501.aspx )
- When the model changes, we have to generate a Code Based Migration otherwise the package will break (Database vs. Model)
- We prefer to remove the creation of migrations from the team (based on https://msdn.microsoft.com/en-us/data/dn481501.aspx)
我已经尝试了网络上的各种操作,但大多数似乎都要求将 AutomaticMigrations
设置为 true
以及 AutomaticMigrationDataLossAllowed
(请参阅:http://romiller.com/2012/02/09/running-scripting-migrations-from-code/ )。
I have tried various things from the web, but most seem to require AutomaticMigrations
to be set to true
as well as AutomaticMigrationDataLossAllowed
(see: http://romiller.com/2012/02/09/running-scripting-migrations-from-code/).
我试图通过.NET反射器来复制 Add-Migration
的功能,但是我似乎找不到调用命令的方法通过Powershell调用的 System.Data.Entity.Migrations.AddMigrationCommand
。
I have tried to replicate what Add-Migration
does by looking through .NET reflector but I can't seem to find a way of invoking the command System.Data.Entity.Migrations.AddMigrationCommand
that is called through Powershell.
任何人都有任何想法我如何才能做到这一点而又不会做得非常凌乱呢?这是我想很多人都想做/已经做过的事情……
Anyone have any ideas at all on how I can get close to achieving this without doing something extremely messy? It's something I think a lot of people will want to do/have done...
在此先感谢!
推荐答案
首先,无法在Visual Studio之外运行Nuget powershell(它使用DTE)。另外,您在没有Visual Studio的情况下编写的所有内容都需要手动插入csproj中(但这不是一项艰巨的任务)。
First of all, no way to run Nuget powershell outside visual studio (it uses DTE). Also, everything you write without Visual Studio need to be inserted in the csproj manually (but not an hard task).
只是为了说明其工作原理,我向您发送了一些代码代码。
要测试它们,请创建MyDll dll(具有上下文和实体的项目测试),然后使用Enable-Migrations在MyDll上手动启用迁移(仅创建Configuration.cs)。
Just to show how it works I send you some line of codes. To test them, create MyDll dll (a project test with context and entities) and then manually enabled migrations on MyDll with Enable-Migrations (just to create Configuration.cs).
之后,您可以使用这段代码来生成源代码
After that you can use this piece of code to generate the source code
DbConnectionInfo connectionStringInfo = new DbConnectionInfo(
"Server=.;Database=MigrationTest;User=sa;Password=dacambiare", "System.Data.SqlClient"); // We shoud retrieve this from App.config
ToolingFacade toolingFacade = new ToolingFacade(
"MyDll", // MigrationAssemblyName. In this case dll should be located in "C:\\Temp\\MigrationTest" dir
"MyDll", // ContextAssemblyName. Same as above
null,
"C:\\Temp\\MigrationTest", // Where the dlls are located
"C:\\Temp\\MigrationTest\\App.config", // Insert the right directory and change with Web.config if required
"C:\\Temp\\App_Data",
connectionStringInfo)
{
LogInfoDelegate = s => {Console.WriteLine(s);},
LogWarningDelegate = s => { Console.WriteLine("WARNING: " + s); },
LogVerboseDelegate = s => { Console.WriteLine("VERBOSE: " + s); }
};
ScaffoldedMigration scaffoldedMigration = toolingFacade.Scaffold("MyMigName", "C#", "MyAppNameSpace", false);
Console.WriteLine(scaffoldedMigration.DesignerCode);
Console.WriteLine("==================");
Console.WriteLine(scaffoldedMigration.UserCode);
// Don't forget the resource file that is in the scaffoldedMigration
EDIT
我忘记了命名空间,并且不经常使用,因此在这里
EDIT
I forgot the namespaces and are not used often so here you are
using System;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Design;
这篇关于以编程方式创建代码优先迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!