【问题标题】:Entity Framework 6 T4 Template change file name of entityEntity Framework 6 T4 模板更改实体的文件名
【发布时间】:2014-07-10 16:55:00
【问题描述】:

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

我的一些表以 a 为前缀,并带有文本“table_”,我想从生成的上下文和 pocos 中删除它。

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

我留下了像 - table_Order.cs 和 table_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; }
<#
    }
#>
    }
}

【问题讨论】:

    标签: entity-framework t4 generated-code


    【解决方案1】:

    只需编辑 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自定义输出文件名!

    对不起。

    【讨论】:

    • 我们可能使用不同的版本,但没有像您的答案中的第一个那样的代码块。
    • 你用的是什么版本?我用过 5 到 6,差别很小。寻找 fileManager.StartNewFile
    • 更好的是,寻找第一个在实体上迭代的 foreach 语句。将实体名称保存在变量中,然后使用常规字符串函数在 fileManager.Process() 之前删除“table_”
    • 好的。该文件正在被另一个 t4 模板调用。您可以通过 Model 和 EntitySet 值已在其他地方设置的事实来判断。你有一个以你的模型命名的,还是在与你的模型相同的文件夹中。它通常被称为 yourModel.tt
    • 我认为如果你安装nuget包EntityFramework.CodeTemplates.CSharp可能会更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多