1
votes

I'm using Entity Framework Power Tools Reverse Engineer Code First to generate my POCO classes, mapping files, and context from the database. I was able to change the T4 templates to generate a different namespace based on my database schema, but I am not able to find how to create a folder based on the tables schema and place the related POCO classes in the folder.

Could somebody help? Thanks

4

4 Answers

1
votes

The folders for the model (and the mappings) are hard-coded in the tool. Reverse-engineering EfPowerTools.dll shows the following lines in method ReverseEngineerCodeFirst of ReverseEngineerCodeFirstHandler:

string str3 = str2 + ".Models";
string path1_1 = Path.Combine(directory.FullName, "Models");
string str4 = str3 + ".Mapping";
string path1_2 = Path.Combine(path1_1, "Mapping");

So, too bad, you can't change the name and location of these folders.

1
votes

I'd have to add another answer as I have tried the approach suggested in my previous one and that didn't work. I have changed EF Power Tools in order to output files to different folder or project.

  1. You need to install the following EF Power Tools Extension (https://entityframework.codeplex.com/SourceControl/network/forks/khorvat/EFPowerToolsEx)

  2. Use this code to accomplish the export

    var efHost = (EfTextTemplateHost)Host;
    var code = new CodeGenerationTools(this);   
    var dte = efHost.DTE;
    EnvDTE.Project ModelProject = null;
    
    foreach(EnvDTE.Project dteProject in dte.Solution)
    {
        if (dteProject.Name.Equals("YourModelProjectName"))
            ModelProject = dteProject;
    }
    
    var ModelProjectDirectory = new FileInfo(ModelProject.FullName).Directory;
    var ModelProjectNamespace = (string)ModelProject.Properties.Item("RootNamespace").Value;
    string ModelNameSpace = ModelProjectNamespace + ".Model";
    string outputPath = Path.Combine(ModelProjectDirectory + ModelExportPath + @"Generated\I" + efHost.EntityType.Name + ".cs"); 
    
    Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
    if (ModelProject.DTE.SourceControl.IsItemUnderSCC(outputPath) && !ModelProject.DTE.SourceControl.IsItemCheckedOut(outputPath))
        ModelProject.DTE.SourceControl.CheckOutItem(outputPath);
    File.WriteAllText(outputPath, this.GenerationEnvironment.ToString());
    ModelProject.ProjectItems.AddFromFile(outputPath);
    this.GenerationEnvironment.Clear();
    

With this you will be able to export output to another file, folder and even a project.

1
votes

Update

As mentioned in other answer this approach won't work. So the answer is no longer applied.

You can try resolving the output path and create a folder by doing the following:

<#@ import namespace="System.IO" #>

var efHost = (EfTextTemplateHost)Host;
var outputPath = Path.Combine(Path.GetDirectoryName(efHost.TemplateFile), "YourFolder");

if (!Directory.Exists(outputPath))         
  Directory.CreateDirectory(outputPath);

Now to output to different folder you can try using the GenerationEnvironment similar to this:

<#@ dte processor="T4Toolbox.DteProcessor" #>
<#@ TransformationContext processor="T4Toolbox.TransformationContextProcessor" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="T4Toolbox" #>

ProcessOutputTemplate template = new ProcessOutputTemplate(this.GenerationEnvironment.ToString());
template.Output.File = outputPath;
template.Render();
this.GenerationEnvironment.Clear();

Note: this approach requires the T4 Toolbox installed in the VS 2012/13 - http://www.olegsych.com/t4toolbox/ (http://www.olegsych.com/t4toolbox/gettingstarted/)

1
votes

I have modified the EFPowerTool extension to support the namespace based directory structure creation. Created a pull request on EF 6.x project at codeplex. Also I have created an experimental branch on github for testing purpose.(There surely are room for fixes/enhancement which can be added and tested before sending updated pull request)

You can download the extension installer with the proposed fix from here(see install dir in source).