您可以更改模板.tt文件,该文件会生成用于使用模型文档属性生成必要属性的类代码。
例如,对于EF5,您可以在*Model.tt方法中将
CodeStringGenerator.Property()
替换为:
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{5} {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)),
(edmProperty.Documentation == null ? "" : ("[Display(Name=\""+edmProperty.Documentation.Summary+"\")]"+Environment.NewLine+" ")));
}
使用 CodeStringGenerator.UsingDirectives()
方法:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}{3}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine,"using System.ComponentModel.DataAnnotations;"+Environment.NewLine)
: "";
}
接下来,在模型和模板中设置Documentation.Summary
属性。通过这种方式,.tt将生成所有包含适当属性的类,而不需要使用元数据类并通过MetadataTypeAttribute将其附加到实体类上。例如:
namespace DataAdmin.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Discount
{
public Discount()
{
this.DiscountPeriods = new HashSet<DiscountPeriod>();
this.Clinics = new HashSet<Clinic>();
this.Doctors = new HashSet<Doctor>();
this.Servs = new HashSet<Serv>();
}
public int DiscountKey { get; set; }
[Display(Name="Discount name")]
public string Name { get; set; }
public string MisCode { get; set; }
public string MisName { get; set; }
public string MisDesc { get; set; }
public decimal Perc { get; set; }
public int Rang { get; set; }
public Nullable<int> DiscountTypeKey { get; set; }
public virtual ICollection<DiscountPeriod> DiscountPeriods { get; set; }
public virtual ICollection<Clinic> Clinics { get; set; }
public virtual ICollection<Doctor> Doctors { get; set; }
public virtual ICollection<Serv> Servs { get; set; }
public virtual DiscountType DiscountType { get; set; }
}
}
MetadataType
并创建带有属性的部分类。请参考以下链接,并告诉我它是否有效:https://dev59.com/5W3Xa4cB1Zd3GeqPeGfR 和 https://dev59.com/IFbUa4cB1Zd3GeqPCdvc - NSGaga-mostly-inactive