实体框架6 T4模板更改实体的文件名

时间:2014-07-08 19:52:29

标签: entity-framework t4 generated-code

我使用EntityFramework.CodeTemplates.CSharp对我的数据库中的表进行反向工程(代码首先来自数据库)。

我的一些表格带有一个带有文字"表格_"的前缀。我想从生成的上下文和pocos中删除它。

在Context.cs.t4和EntityType.cs.t4中,我能够对生成的c#代码进行我想要的更改,但是我无法看到如何更改生成的文件名本身。

我留下了像table_Order.cstable_OrderItem.cs

这样的文件

以下是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; }
<#
    }
#>
    }
}

1 个答案:

答案 0 :(得分:2)

只需编辑t4模板并手动编辑文件名即可。 例如,它说:

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

您可以将其更改为:

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");

修改

抱歉,我没有意识到您使用的是powertools模板。

运行模板后,您可能必须手动执行此操作。在我的情况下,我添加了类似于模板创建结束的内容:

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

最终,使用this extension

可能会更好

修改2

首先对逆向工程代码进行自定义输出文件名似乎it still isn't possible

对不起。