3

自2010年以來,我一直在使用實體框架模型。當我構建我的項目時,EF生成一個包含所有實體的Model.Designer.cs文件。此設計器文件還包含添加到EDMX文件中的實體的文檔。將文檔添加到實體框架模型中的生成代碼中

當我在VS 2012中創建一個新的EF模型第一個項目時,Model.tt文件被添加到我的EDMX文件中。這個T4模板爲我的模型中的每個實體生成一個文件。不幸的是,EDMX文件中的文檔沒有在生成的代碼中使用。

我真的很喜歡將我的模型記錄下來,以便在使用IntelliSense時顯示出來。目前唯一的解決方法是移除Model.tt和生成的類文件,並重新開啓我的EDMX文件上的代碼生成。這將恢復到我在VS 2010中使用的行爲。但是,我希望每個實體都有一個單獨的文件。

有什麼方法(最好使用VS工具,而不必修改VS附帶的任何文件)將EDMX文件中的文檔包含在生成的單個類文件中?

編輯:爲了進一步說明我的問題,下面是一個簡單的例子。

比方說,我的模型是這樣的:simple entity framework model

我強調,我進入了id屬性的屬性窗口中的文件的一部分。

這是實體的樣子在EDMX文件:

<EntityType Name="Entity1"> 
     <Key> 
     <PropertyRef Name="Id" /> 
     </Key> 
     <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" > 
     <Documentation> 
      <Summary>This is documentation for the ID property.</Summary> 
     </Documentation> 
     </Property> 
    </EntityType> 

通過Model.tt生成的類(Entity1.cs)看起來是這樣的:

public partial class Entity1 
{ 
    public int Id { get; set; } 
} 

但是當我打開在我的模型的代碼生成中,這是Model.Designer.cs中實體的樣子:

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[EdmEntityTypeAttribute(NamespaceName="Model1", Name="Entity1")] 
[Serializable()] 
[DataContractAttribute(IsReference=true)] 
public partial class Entity1 : EntityObject 
{ 
    #region Factory Method 

    /// <summary> 
    /// Create a new Entity1 object. 
    /// </summary> 
    /// <param name="id">Initial value of the Id property.</param> 
    public static Entity1 CreateEntity1(global::System.Int32 id) 
    { 
     Entity1 entity1 = new Entity1(); 
     entity1.Id = id; 
     return entity1; 
    } 

    #endregion 

    #region Simple Properties 

    /// <summary> 
    /// This is documentation for the ID property. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int32 Id 
    { 
     get 
     { 
      return _Id; 
     } 
     set 
     { 
      if (_Id != value) 
      { 
       OnIdChanging(value); 
       ReportPropertyChanging("Id"); 
       _Id = StructuralObject.SetValidValue(value, "Id"); 
       ReportPropertyChanged("Id"); 
       OnIdChanged(); 
      } 
     } 
    } 
    private global::System.Int32 _Id; 
    partial void OnIdChanging(global::System.Int32 value); 
    partial void OnIdChanged(); 

    #endregion 

} 

所以你看到:Model.Designer.cs包含我的自定義文檔字符串「這是ID屬性的文檔。」而Entity1.cs則不。但是,如果有很多實體並且調試到這個文件有點慢,Model.Designer.cs會變得非常大。我更喜歡有幾個小文件(每個實體一個),但仍保留生成的代碼中EDMX文件的文檔。

+1

您必須更改T4文件以在生成的代碼中包含文檔。也許你可以發佈T4文件? – knaki02

+0

我可以發佈T4文件,但那會很長。此外,我沒有改變這一點。這是由VS提供的標準文件。我也不希望改變這一點,因爲我認爲我必須爲每個項目都這樣做。 VS更新更改文件也將丟失。 – Chris

+0

您可以修改將在您的下一個項目中使用的模板文件... – knaki02

回答

13

我認爲你將不得不修改T4文件。我有同樣的問題,並通過T4文件讀了一下,並試圖按照這裏的說明:http://karlz.net/blog/index.php/2010/01/16/xml-comments-for-entity-framework/

但是,我們使用VS 2012和該指令似乎不工作100%。我最終改變了T4文件末尾的屬性生成代碼,它的工作原理就是我想要的。這些變化是在CodeStringGenerator.Property()和CodeStringGenerator.NavigationProperty()

public string Property(EdmProperty edmProperty) 
{ 
    string doc = ""; 
    if (edmProperty.Documentation != null) 
    { 
     doc = string.Format(
     CultureInfo.InvariantCulture, 
     "\n\t\t/// <summary>\n\t\t/// {0} - {1}\n\t\t/// </summary>\n\t\t", 
     edmProperty.Documentation.Summary ?? "", 
     edmProperty.Documentation.LongDescription ?? ""); 
    } 

    return doc + string.Format(
     CultureInfo.InvariantCulture, 
     "{0} {1} {2} {{ {3}get; {4}set; }}", 
     Accessibility.ForProperty(edmProperty), 
     _typeMapper.GetTypeName(edmProperty.TypeUsage), 
     _code.Escape(edmProperty), 
     _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), 
     _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); 
} 

public string NavigationProperty(NavigationProperty navigationProperty) 
{ 
    var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType()); 
    string doc = ""; 
    if (navigationProperty.Documentation != null) 
    { 
     doc = string.Format(
     CultureInfo.InvariantCulture, 
     "\n\t\t/// <summary>\n\t\t/// {0} - {1}\n\t\t/// </summary>\n\t\t", 
     navigationProperty.Documentation.Summary ?? "", 
     navigationProperty.Documentation.LongDescription ?? ""); 
    } 

    return doc + string.Format(
     CultureInfo.InvariantCulture, 
     "{0} {1} {2} {{ {3}get; {4}set; }}", 
     AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)), 
     navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, 
     _code.Escape(navigationProperty), 
     _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), 
     _code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); 
} 

注意,它不會與類的文檔工作,所以你必須做這樣的事情,實體和複雜類型

<#=codeStringGenerator.UsingDirectives(inHeader: false)#> 
<#if (!ReferenceEquals(entity.Documentation, null)) 
{ 
#> 
/// <summary> 
/// <#=entity.Documentation.Summary#> – <#=entity.Documentation.LongDescription#> 
/// </summary> 
<#}#> 
<#=codeStringGenerator.EntityClassOpening(entity)#> 
相關問題