2014-07-08 81 views
2

我正在使用EntityFramework.CodeTemplates.CSharp對我的數據庫中的表進行逆向工程(代碼先從數據庫中獲得)。實體框架6 T4模板更改實體的文件名

我的一些表前面加了一個帶有文本「table_」的文本,我想從生成的上下文和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; } 
<# 
    } 
#> 
    } 
} 

回答

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

編輯

對不起,我沒你使用電動工具模板實現。

運行模板後,您可能必須手動執行此操作。在我來說,我已經添加了類似我的模板創建的最後一句:

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定製更好輸出文件名稱時逆向工程代碼第一!

對不起。

+0

我們可能會使用不同的版本,但是沒有像第一個答案中的代碼塊。 – Bryan

+0

你使用什麼版本?我已經從5點到6點使用,差異很小。尋找fileManager.StartNewFile – reckface

+0

更好,尋找迭代在實體上的第一個foreach語句。將實體名稱保存在一個變量中,然後使用常規字符串函數去除fileManager.Process()前的「table_」。 – reckface