2

I'm using EntityFramework.CodeTemplates.CSharp to reverse engineer the tables in my database (code first from database).

Some of my tables a prefixed with a with the text "table_" and I want to remove that from the generated context and pocos.

In the Context.cs.t4 and EntityType.cs.t4 I am able to make the changes I want to the generated c# code, but I can't see how to change the generated file name itself.

I am left with files like - table_Order.cs and table_OrderItem.cs

Here is the code within EntityType.cs.t4

<#@ template visibility="internal" linePragmas="false" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.Data.Entity.Design" #>
<#@ assembly name="EntityFramework" #>
<#@ import namespace="System.Data.Entity.Core.Metadata.Edm" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Microsoft.Data.Entity.Design.CodeGeneration" #>
<#@ parameter type="System.Data.Entity.Core.Metadata.Edm.EntitySet" name="EntitySet" #>
<#@ parameter type="System.Data.Entity.Infrastructure.DbModel" name="Model" #>
<#@ parameter type="System.String" name="Namespace" #>
<#
    var code = new CSharpCodeHelper();
    var edm = new EdmHelper(code);

    if (EntitySet == null)
    {
        throw new ArgumentNullException("EntitySet");
    }

    if (Model == null)
    {
        throw new ArgumentNullException("Model");
    }

    var entityType = EntitySet.ElementType;
#>
namespace <#= Namespace #>
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

<#
    var typeConfigurations = edm.GetConfigurations(EntitySet, Model).OfType<IAttributeConfiguration>();

    foreach (var typeConfiguration in typeConfigurations)
    {
#>
    <#= code.Attribute(typeConfiguration) #>
<#
    }
#>
    public partial class <#= code.Type(entityType) #>
    {
<#
    var collectionProperties = from p in entityType.NavigationProperties
                               where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                               select p;

    if (collectionProperties.Any())
    {
#>
        public <#= code.Type(entityType) #>()
        {
<#
    foreach (var collectionProperty in collectionProperties)
    {
#>
            <#= code.Property(collectionProperty) #> = new HashSet<<#= code.Type(collectionProperty.ToEndMember.GetEntityType()) #>>();
<#
    }
#>
        }

<#
    }

    var first = true;

    foreach (var property in entityType.Properties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

        var propertyConfigurations = edm.GetConfigurations(property, Model).OfType<IAttributeConfiguration>();

        foreach (var propertyConfiguration in propertyConfigurations)
        {
#>
        <#= code.Attribute(propertyConfiguration) #>
<#
        }
#>
        public <#= code.Type(property) #> <#= code.Property(property) #> { get; set; }
<#
    }

    foreach (var navigationProperty in entityType.NavigationProperties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

#>
        public virtual <#= code.Type(navigationProperty) #> <#= code.Property(navigationProperty) #> { get; set; }
<#
    }
#>
    }
}
Bryan
  • 5,065
  • 10
  • 51
  • 68

1 Answers1

2

Just edit the t4 template and edit the file name manually. For example where it says:

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

you can change that to:

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    var entityName = entity.Name;
    if(entityName.StartWith("table_")) entityName = entityName.Replace("table_", string.Empty);
    fileManager.StartNewFile(entityName + ".cs");

EDIT

Sorry, I didn't realise you were using the powertools template.

You may have to do this manually after running the templates. In my case I've added something similar to the end of my template creation:

DirectoryInfo di = new DirectoryInfo("");
var files = di.GetFiles("table_*.cs");
foreach (var fi in files.ToArray())
{
    fi.MoveTo(fi.FullName.Replace("table_",string.Empty));
}

Ultimately, you might be better off with this extension

Edit 2

It appears it still isn't possible to customise the output file name when reverse engineering code first!

Sorry.

Community
  • 1
  • 1
reckface
  • 5,678
  • 4
  • 36
  • 62
  • We might be using different versions, but there is no code block like the first one in your answer. – Bryan Jul 09 '14 at 14:09
  • What version do you use? I've used from 5 to 6, and the differences are minor. Look for fileManager.StartNewFile – reckface Jul 09 '14 at 14:33
  • better yet, look for the first foreach statement iterating on the entities. hold the entity name in a variable, and then use regular string functions to strip out the "table_" before fileManager.Process() – reckface Jul 09 '14 at 14:35
  • Ok. This file is being called by another t4 template. You can tell by the fact that Model and EntitySet values have already been set elsewhere. Do you have the one named after your model, or in the same folder as your model. It will normally be called something like yourModel.tt – reckface Jul 09 '14 at 15:39
  • 1
    I think it might be easier if you install the nuget package EntityFramework.CodeTemplates.CSharp. – Bryan Jul 09 '14 at 20:11