You can change template .tt file, wich generates classes code to use Documentation property of your model for generating classes with nessesary attributes.
For example for EF5 you can replace in *Model.tt method CodeStringGenerator.Property()
by:
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+" ")));
}
And CodeStringGenerator.UsingDirectives()
with:
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)
: "";
}
After that set Documentation.Summary
property in model and template .tt will generate all classes with proper attributes without using metadata class and attaching it to your entity class via the MetadataTypeAttribute.
For example:
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
- and making a partial class with attributes - if that's the issue you're facing - take a look at these links - let me know how it works - #14412968 - #5889849 – Sigler