使用MSBuild构建实体框架模型而不使用项目文件? [英] Use MSBuild to build Entity Framework Model without using the Project File?

查看:179
本文介绍了使用MSBuild构建实体框架模型而不使用项目文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个相当大的项目构建一个脚本的过程。其中一个项目需要一个实体框架模型被编译成库。由于构建服务器的工作方式,所有构建都是手工制作的,以管理各种部署方案,而不会影响开发人员和项目文件。

I am in the process of build a script for a fairly big project. One of the projects require an Entity Framework Model to be compiled into a library. Due to the way the build server works, all builds are hand crafted to manage the various deployment scenarios without affecting the developers and the project files.

EF模型使用MSBuild任务,而不使用Visual Studio生成的项目文件?目前实际的程序集使用CSC任务编译,但实际部署的应用程序失败,因为EF模型不包括在编译程序集中。

Is there anyway to generate the EF Model using an MSBuild task without using the project file generated by Visual Studio? Currently the actual assembly compiles using a CSC task, however the actual deployed application fails because the EF Model is not included in the compiled assembly.

任何指针,任务可用于生成模型并创建嵌入到程序集中所需的资源。

Any pointers into which Targets or Build Tasks can be used to generate the model and create the required resources to embed in the assembly?

请注意

项目在Visual Studio中编译得很好,但是使用构建服务器上的项目文件不是一个选项,因为在部署时需要对项目进行各种更改,这是通过在开发团队控制之外的自定义构建脚本控制。

The project compiles fine in Visual Studio, however using the project file on the build server is not an option, as there is various changes that needs to be made to the project at deployment time, and this is controlled through a custom build script outside of the development teams control. This has worked succesfully for a few projects, however the EF Model is causing some headaches in this specific scenario.

更新

当前的解决方案不理想,但工作。项目文件已修改为将EF模型资源复制到项目中新版本构建的文件夹中,然后将其检入源代码控制。当构建脚本在服务器上运行时,EF模型被嵌入到程序集中。这似乎是一个可行的解决方法。

The current solution is not ideal but works. The Project file has been modified to copy the EF Model resources to a folder in the project on a new release build, which is then checked into source control. When the build script runs on the server the EF models are embedded into the assembly. This seems to be a workable workaround for the moment.

推荐答案

我实际上处理了今天,因为我的项目的源代码树正确设置运行Entity Framework的生成任务,生成并在输出dll中嵌入资源。

I actually dealt with this today since my project's source tree isn't properly setup to run the Entity Framework's build task which generates and embeds the resources in the output dll.

我调查了以下选项:

使用EntityDeploy msbuild任务

如果在记事本中打开.csproj文件,

If you open your .csproj file in notepad you should see a bit of XML like

 <EntityDeploy Include="MyEntities.edmx">
  <Generator>EntityModelCodeGenerator</Generator>
  <LastGenOutput>MyEntities.Designer.cs</LastGenOutput>
 </EntityDeploy>

这是msbuild任务,它使用 Microsoft.Data.Entities.Build.targets Microsoft.Data.Entities.Build.dll 以读取edmx文件,生成ssdl,csdl和msl文件,然后将其嵌入到目标dll中。 (这些文件可以在 C:\Windows \Microsoft.NET\Framework\v3.5 中找到)。

This is the msbuild task that uses Microsoft.Data.Entities.Build.targets and Microsoft.Data.Entities.Build.dll to read the edmx file, generate the ssdl, csdl, and msl files and then embed them in the target dll. (These files can be found in C:\Windows\Microsoft.NET\Framework\v3.5).

使用EdmGen

正如Craig指出的,您可以使用该框架附带的 EdmGen.exe 。我给了一个去,但我做了一些定制的数据在我的edmx文件和EdmGen.exe似乎真的想从原始数据库做初始生成。

As Craig pointed out, you might be able to use EdmGen.exe which ships with the framework. I gave that a go but I had done a bit of customization to the data in my edmx file and EdmGen.exe seems to really want to do the initial generation from the original database.

使用EdmGen2

EdmGen2.exe 是一个开源项目,比EdmGen有点多。我最终使用它来生成ssdl,csdl和msl文件。您可以将其指向您的edmx文件,它将生成必需的ssdl,csdl和msl文件。然后我包括这些在我的dll和修改连接字符串

EdmGen2.exe is an open-source project that does a bit more than EdmGen. I ended up using it to generate the ssdl, csdl, and msl files. You can just point it at your edmx file and it will generate the necessary ssdl, csdl, and msl files. I then included these alongside my dll and modified the connection string from

connectionString="metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|res://*/MyEntities.msl;

connectionString="metadata=./MyEntities.csdl|./MyEntities.ssdl|./MyEntities.msl;

注意我告诉Entity框架这些文件是在我的dll,作为资源嵌入。

Note I'm telling the Entity framework that these files are in the same directory as my dll, not embedded as resources.

所有这三个,构建任务,EdmGen和EdmGen2是薄包装器,调用System.Data.Entity.dll的所有硬的东西。作为最后一个手段,你可以在反射器中查看构建任务dll,看看它在做什么。

All three of these, the build task, EdmGen, and EdmGen2 are thin wrappers that call System.Data.Entity.dll for all of the hard stuff. As a last resort you could peek at the build task dll in reflector and see what it's doing.

希望这有助于。

这篇关于使用MSBuild构建实体框架模型而不使用项目文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆