Meta Data - Annotating Models
One of the issues with using Entity Framework is that you may want some special annotation but you don't want to use a DTO or other object outside of EF.
One way to afford you the opportunity to Annotate on the original EF model is to extend with a partial class of meta data.
In your Data Models folder (or really anywhere in your data project) you would add a class (Partial Classes) that would contain empty pointers to your data classes - (in my tt files I have a company class and a systemUser class which represent models from the tables in my database):
Once you have this setup you can begin to annotate! Create a systemUsersMetadata.cs class and you can now add annotations while still using your original EF model - and you don't have to annotate everything - just the fields you want.
One way to afford you the opportunity to Annotate on the original EF model is to extend with a partial class of meta data.
In your Data Models folder (or really anywhere in your data project) you would add a class (Partial Classes) that would contain empty pointers to your data classes - (in my tt files I have a company class and a systemUser class which represent models from the tables in my database):
PartialClasses.cs
using System.ComponentModel.DataAnnotations;
namespace MySite.Data
{
[MetadataType(typeof(systemUsersMetadata))]
public partial class systemUser
{
}
[MetadataType(typeof(companyMetadata))]
public partial class company
{
}
}
|
Once you have this setup you can begin to annotate! Create a systemUsersMetadata.cs class and you can now add annotations while still using your original EF model - and you don't have to annotate everything - just the fields you want.
public class systemUsersMetadata { [RegularExpression(@"^[^<>*]+$", ErrorMessage = "Special characters are not allowed")] [Required(ErrorMessage = "First Name is required")] [MaxLength(30, ErrorMessage = "First Name may not exceed 30 characters")] [Display(Name = "First Name")] public string firstName { get; set; } [RegularExpression(@"^[^<>*]+$", ErrorMessage = "Special characters are not allowed")] [Display(Name = "Last Name")] [Required(ErrorMessage = "Last Name is required")] [MaxLength(30, ErrorMessage = "Last Name may not exceed 30 characters")] public string lastName { get; set; } [Display(Name = "Middle Name")] public string middleName { get; set; } [Display(Name = "Email")] [DataType(DataType.EmailAddress)] [Required(ErrorMessage = "Email is required")] [MaxLength(60, ErrorMessage = "Email Address may not exceed 60 characters")] public string email { get; set; } [Display(Name = "Phone")] [RegularExpression(@"^[^<>*]+$", ErrorMessage = "Special characters are not allowed")] [Required(ErrorMessage ="Phone is required.")] [MaxLength(15, ErrorMessage = "Phone number may not exceed 15 characters")] public string phone { get; set; } } |
Comments