2

EntityFramework.CodeTemplates.CSharp を使用して、データベース内のテーブルをリバース エンジニアリングしています (最初にデータベースからコードを作成します)。

私のテーブルのいくつかは、「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; }
<#
    }
#>
    }
}
4

1 に答える 1

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

最終的には、この拡張機能を使用した方が良いかもしれません

編集 2

最初にコードをリバース エンジニアリングするときに、出力ファイル名をカスタマイズすることはまだできないようです。

ごめん。

于 2014-07-09T11:53:42.287 に答える