Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Model code from Model.edmx in EF6 without Visual Studio

For our application, we do not use Visual Studio in the build process; Visual Studio is not on the build servers. In general, we have kept Visual Studio as a "nice to have" for developing the application, but the crucial requirements for a reproducible build are all open source / freely available.

I am now considering upgrading to EF6. I do not want to check in the generated Model code, so I need a way of doing Model generation from the edmx file without requiring Visual Studio.

Previously, we have used EdmGen, as that is shipped with the .NET framework, and we are happy for our build servers to depend on that. In EF6, the recommended way to generate the code is using T4 templates.

However, the T4 code generation tools are built into Visual Studio and are not shipped separately.

This question contains part of the answer. However, there are problems with both answers.

Firstly, it is suggested that the licence allows you to copy the necessary files to the build server from your Visual Studio installation. However, this is an ugly solution, as it means setting a up a new build server can't be automated, and depends upon a Visual Studio installation being present. Additionally, for EF6 model generation, we also need the EF.Utility.CS.ttinclude in the Visual Studio installation. I have an email from one of the Microsoft devs saying that in his non-legal opinion, I would be allowed to copy that file too, but looking at the licence, I am not convinced.

Secondly, and much more attractively, there is an open-source implementation of TextTransform in MonoDevelop. However, again, we are missing that include file, and additionally, it's not clear to me that this TextTransform tool works exactly the same way, and can handle the template that EF6 has given me. So far I have errors trying to get it to work.

So: Does anyone have a working example of doing EF6 model generation without using tools that can only be got from a Visual Studio installation?

like image 400
Martin Eden Avatar asked Jan 03 '14 16:01

Martin Eden


People also ask

How is EDMX file generated?

edmx is basically an XML file which is generated when we added Entity Framework model. It is Entity Data Model Xml which contains designer (Model) and code file(. cs).

Do we need EDMX XML file for EF code First approach?

Code first approach lets us transform our coded classes into database application, which means code first lets us to define our domain model using POCO (plain old CLR object) class rather than using an XML-based EDMX files which has no dependency with Entity Framework.

How do I manually edit EDMX?

edmx file open in Visual Studio, you should be able to simply right-click on a table or a column in the table and choose 'rename'. Once you change the name it will be reflected in the Mapping Details window. Problem is that's editing the entity names and properties, not the underlying SQL tables and columns.


1 Answers

I now have this working. The solution I finished with was using the open-source implementation of TextTransform from MonoDevelop. It turns out that the template include files used by the tt template created by Entity Framework are also available under an open-licence. They are available from here.

In order to get it working with the template created by Visual Studio, I had to make the some changes.

In Model.tt, I had to remove the #if PREPROCESSED_TEMPLATE check in GetNamespaceName.

Secondly, I had to make some small modifications to the include file. The full modified file is here.

All the files you need to do this yourself are in that repository, although note that I don't guarantee that the binaries of TextTransform that I have included there are up to date - you should probably build your own / grab them out of a MonoDevelop install. Just add a Model.tt file, and invoke using:

./TextTransform.exe -out Models.cs -I INCLUDES Model.tt

where "INCLUDES" is the folder with the modified EF.Utility.CS.ttinclude in.

like image 174
Martin Eden Avatar answered Nov 01 '22 12:11

Martin Eden