1

with the Tutorial: http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/creating-the-web-application I created a Entitiy-Framework Version 6 File (.edmx), with the sub-files. This File is in my project: SimpleSalesOrder.DataLayer.SQL.

Everything fine so far, but I'd like to generate the Business-Model Classes in our SimpleSalesOrder.BusinessModel.SQL Project to seperate the DataLayer from the BusinessModels.

I'm quite sure I have to edit the T4-Template to make this work. The Namespace itself is no problem since I can enter this when I create the EF-Contect.

I found the Point, where the Files get created:

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))    
{
     fileManager.StartNewFile(entity.Name + ".cs");
     BeginNamespace(code);
#>

But as you cann see, It passes only the Class-Name. The Output itself is setted in the Process Procedure:

var outputPath = Path.GetDirectoryName(_textTransformation.Host.TemplateFile);

So there is no Problem to kindahow set this Name to the Name of the BusinessModel. But is this the way to do it? Is there a easier way to make this work? I could imagine many people would like to seperate the BusinessModels from the DataLayer.

Thanks in advance and a good night!

Matthias

Matthias Müller
  • 3,336
  • 3
  • 33
  • 65

1 Answers1

0

To simplify, your requirement: Generate files based on an entity model that is defined in project A into a project B.

It looks like your main issue here is that your T4 template file is in project A.

The simple solution is to move your T4 file into project B, update the path to your .edmx file (or if you are using EF Code first, update your assembly references) to point to the physical path of the EF source in project A.

Hint: use relative paths if you can, but fully qualified paths will also work.

Issue: the .ttinclude that you are using is designed to generate files in the same folder path as the template file, in the VS solution explorer the generated files will be nested under the template file.

You could modify the ttinclude file, but you are over-complicating the issue.

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
  • NOTE: It is not necessary for Project B to reference Project A, the T4 template executes outside of the scope of your project, any necessary references and includes are defined in the header of the template file itself. Think of the template executing as it's own application. – Chris Schaller Jan 08 '16 at 00:44