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

c# entity-framework-6 mono monodevelop t4

Question

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?

Accepted Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why