Luk*_*ley 14 c# powershell entity-framework
我正处于一个项目中,我们在实体框架上使用Code First作为我们的数据库.
我们希望改变所有持续集成以消耗下游生成的MSI包,但使用EF会带来一些复杂性.
我已尝试过网络上的各种内容,但大多数似乎都需要AutomaticMigrations设置true为AutomaticMigrationDataLossAllowed(参见:http://romiller.com/2012/02/09/running-scripting-migrations-from-code/).
我试图Add-Migration通过查看.NET反射器来复制所做的事情,但我似乎无法找到一种调用System.Data.Entity.Migrations.AddMigrationCommand通过Powershell 调用的命令的方法.
任何人都有任何想法,我怎么能接近实现这一点,而不做一些非常混乱的事情?这是我认为很多人都想要做的事情......
提前谢谢了!
首先,没有办法在visual studio之外运行Nuget powershell(它使用DTE).此外,您在没有Visual Studio的情况下编写的所有内容都需要手动插入csproj(但不是一项艰巨的任务).
只是为了说明它是如何工作的,我发给你一些代码.要测试它们,请创建MyDll dll(带有上下文和实体的项目测试),然后使用Enable-Migrations在MyDll上手动启用迁移(仅用于创建Configuration.cs).
之后,您可以使用这段代码生成源代码
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
Run Code Online (Sandbox Code Playgroud)
编辑
我忘记了名称空间,并没有经常使用所以你在这里
using System;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Design;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1945 次 |
| 最近记录: |