以编程方式调用EntityDeploy构建任务

时间:2015-11-27 08:10:37

标签: c# .net entity-framework msbuild

我使用Roslyn编译,发出和运行C#源代码。但是,在面对使用EntityFramework的项目时,我遇到了限制。

似乎只是简单地发出编译是不够的,因为有一个.csproj构建任务在它们被发出后操纵它们。 (我相信它会在它们被发出之后将元数据工件嵌入到DLL中。)

在我处理的<EntityDeploy Include="Models\Northwind.edmx"> <Generator>EntityModelCodeGenerator</Generator> <LastGenOutput>Northwind.Designer.cs</LastGenOutput> </EntityDeploy> 文件中,我看到以下实体部署任务:

msbuild.exe

是否可以直接调用此构建任务并操纵我发出的DLL?

注意:我不想简单地致电MSBuild或对.csproj文件中的所有内容运行Microsoft.Build.Evaluation。我构建的项目存在于内存中,但不存在于磁盘上,因此在我的案例中不会起作用。

我尝试了什么:

我试图学习如何使用var project = new Project(@"C:\Users\JoshVarty\Documents\Visual Studio 2015\Projects\WebApplication1\WebApplication1\WebApplication1.csproj"); //Get the entity deploy target? I'm not sure if this is a task or target. var entityDeploy = project.Targets.Where(n => n.Key == "EntityDeploy").Single(); var projectTargetInstance = entityDeploy.Value; 内容。我可以找到EntityDeploy任务,但我对如何调用它(以及我应该提供的参数)感到茫然。

EntityDeploy

我还尝试查看磁盘上存在的var entityDeployTask = new Microsoft.Data.Entity.Build.Tasks.EntityDeploy(); entityDeployTask.Sources = //I'm not sure where I can get the ITaskItem[] I need entityDeployTask.EntityDataModelEmbeddedResources = //I'm not sure where I can get the ITaskItem[] entityDeployTask.Execute(); 构建任务。

MSBuild

我同时对EntityFrameworkEntityDeploy和{{1}}提出了新的要求,如果我误用了条款或完全以错误的方式来解决这个问题,请纠正我

2 个答案:

答案 0 :(得分:4)

我不熟悉EntityDeploy,但我会提供一些我可能会帮助您的信息。

如果您查看targets file,就可以看到EntityDeploy 目标依赖于EntityDeployEntityDeploySplitEntityDeploySetLogicalNamesEntityClean 任务

当使用EntityDeploy文件列表调用.edmx目标时,会发生以下情况:

  1. EntityDeploySplit被调用,它会读取.edmx个文件,并确定处理每个文件的结果是应嵌入目标程序集中还是放在一起。
  2. EntityDeployNonEmbeddingItems从1开始调用,它会分割.edmx个文件并将结果放入OutputPath
  3. EntityDeployEmbeddingItems从1开始调用,它会分割.edmx个文件并将结果放入EntityDeployIntermediateResourcePath
  4. EntityDeployIntermediateResourcePath中的文件调用
  5. EntityDeploySetLogicalNames,将每个文件的元数据LogicalName设置为用于嵌入的逻辑名称(它只是相对路径)来自EntityDeployIntermediateResourcePath的文件,其中斜线替换为点)
  6. 调用
  7. EntityClean删除中间文件
  8. 我还没试过这个,但应该可以按顺序调用这些来使用Engine class获得预期的行为:

     // Instantiate a new Engine object
     Engine engine = new Engine();
    
     // Point to the path that contains the .NET Framework 2.0 CLR and tools
     engine.BinPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System)
    + @"\..\Microsoft.NET\Framework\v2.0.50727";
     var entityDeploySplitTask = new EntityDeploySplit();
     entityDeploySplitTask.Sources = new ITaskItem[] 
     {
       new TaskItem("Model.edmx")// path to .edmx file from .csproj
     };
     entityDeploySplitTask.BuildEngine = engine;
     entityDeploySplitTask.Execute();
    
     var entityDeployTask = new EntityDeploy();
     entityDeployTask.Sources = entityDeploySplitTask.NonEmbeddingItems
     entityDeployTask.OutputPath = "."; // path to the assembly output folder
     entityDeployTask.BuildEngine = engine;
     entityDeployTask.Execute();
    
     var entityDeployTask2 = new EntityDeploy();
     entityDeployTask2.Sources = entityDeploySplitTask.EmbeddingItems
     entityDeployTask2.OutputPath = "C:\Temp"; // path to an intermediate folder
     entityDeployTask2.BuildEngine = engine;
     entityDeployTask2.Execute();
    
     var entityDeploySetLogicalTask = new EntityDeploySetLogicalNames();
     entityDeploySetLogicalTask.Sources = Directory.EnumerateFiles("C:\Temp", "*.*", SearchOption.AllDirectories)
         .Select(f => new TaskItem(f)).ToArray();
     entityDeploySetLogicalTask.ResourceOutputPath = "C:\Temp"; // path to the intermediate folder
     entityDeploySetLogicalTask.BuildEngine = engine;
     entityDeploySetLogicalTask.Execute();
    
     foreach(var resourceFile in entityDeploySetLogicalTask.ResourcesToEmbed)
     {
        var fileName = resourceFile.GetMetadata("Identity");
        var logicalName = resourceFile.GetMetadata("LogicalName");
        //TODO: Embed filename using logicalName in the output assembly
        //You can embed them as normal resources by passing /resource to csc.exe
        //eg. /resource:obj\Debug\edmxResourcesToEmbed\Models\SampleEF.csdl,Models.SampleEF.csdl
     }
    
     //TODO: call EntityClean or just remove all files from the intermediate directory
    

答案 1 :(得分:1)

尝试以下方法:

var mockObject = new Mock<IBuildEngine>();
IBuildEngine engine = mockObject.Object;

var entityDeployTask = new EntityDeploy();
entityDeployTask.Sources = new ITaskItem[] 
{
  new TaskItem(@"path to edmx\Model1.edmx")
};
entityDeployTask.OutputPath = @"C:\";
entityDeployTask.BuildEngine = engine;
entityDeployTask.Execute();

输出路径似乎没有被拾取,但如果它是空的,则会记录错误。如果您实现自己的IBuildEngine并记录错误,则可以看到此信息。该过程的结果将是edmx旁边的三个文件:&#34; Model1.ssdl&#34;,&#34; Model1.csdl&#34;,&#34; Model1.msdl&#34;。这些文件需要作为嵌入资源传递给CSC,至少原始targets文件似乎是这样做的。

希望它有所帮助,至少可以让你开始。