我正在使用 Roslyn 编译、生成和运行 C# 源代码。但是,在面对使用 EntityFramework 的项目时,我遇到了一个限制。
似乎仅仅发出编译是不够的,因为有一个 EntityDeploy
构建任务可以在 DLL 发出后对其进行操作。 (我相信它是在 DLL 发出后将元数据工件嵌入到 DLL 中)。
在我正在处理的 .csproj
文件中,我看到以下实体部署任务:
<EntityDeploy Include="Models\Northwind.edmx">
<Generator>EntityModelCodeGenerator</Generator>
<LastGenOutput>Northwind.Designer.cs</LastGenOutput>
</EntityDeploy>
是否可以直接调用此构建任务并操作我发出的 DLL?
注意:我不想简单地调用 msbuild.exe
或对 .csproj< 中的所有内容运行
文件。我正在构建的项目存在于内存中,但不在磁盘上,因此这对我来说不起作用。MSBuild
/
我尝试过的:
我正在尝试学习如何使用 Microsoft.Build.Evaluation
东西。我可以找到 EntityDeploy 任务,但我不知道如何调用它(以及我应该提供哪些参数)。
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
构建任务。
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
、EntityFramework
和 EntityDeploy
的新手,所以如果我误用了术语或来自这完全是错误的方式。
最佳答案
我不熟悉 EntityDeploy
,但我会提供一些我收集到的可能对您有所帮助的信息。
如果您查看 targets file你可以看到有一个 EntityDeploy
target 依赖于 EntityDeploy
, EntityDeploySplit
, EntityDeploySetLogicalNames
和 EntityClean
任务。
当使用 .edmx
文件列表调用 EntityDeploy
目标时,会发生以下情况:
- EntityDeploySplit被调用时,它会读取
.edmx
文件并确定处理每个文件的结果是应该嵌入到目标程序集中还是放在一起。 - EntityDeploy从 1. 在
NonEmbeddingItems
上调用,它拆分.edmx
文件并将结果放在OutputPath
中
- EntityDeploy从 1. 在
EmbeddingItems
上调用,它拆分.edmx
文件并将结果放在EntityDeployIntermediateResourcePath
- EntityDeploySetLogicalNames在
EntityDeployIntermediateResourcePath
中的文件上调用以将每个文件上的元数据LogicalName
设置为用于嵌入的逻辑名称(它只是文件的相对路径EntityDeployIntermediateResourcePath
用点替换斜线) - EntityClean调用删除中间文件
我还没有尝试过这个,但是应该可以按顺序调用它们以获得使用 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
关于c# - 以编程方式调用 EntityDeploy 构建任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952883/