您可以更改模板 .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)
: "";
}
在模型和模板.tt中设置Documentation.Summary
属性之后,将生成具有适当属性的所有类,而不使用元数据类并通过 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; }
}
}