This is kind of hidden I agree. The Entity Framework layer has some additional functionality. There is a "buddy" class associated with each entity. These are metadata classes that describe the data within each entity. EF entities can have a meta class associated
with the "System.ComponentModel.DataAnnotations.MetadataType" attribute on the entity class. We are using this to get the regular expression off of the entity and dynamically create a regular express validator in a web UI. There will be an article published
on CodeProject.com soon. Here is a code snippet of how we are pulling the expression out. It does require reflection.
private string GetRegularExpression(Widgetsphere.EFCore.DataAccess.NHEntityObject entity, Enum field)
var o = entity.GetMetaData();
var a = o.GetType().GetField(field.ToString()).GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.RegularExpressionAttribute), true).FirstOrDefault();
if (a == null) return string.Empty;
else return ((System.ComponentModel.DataAnnotations.RegularExpressionAttribute)a).Pattern;
The NHEntityObject is the base object for all entities generated by nHydrate. We take this in and with reflection pull out the RE from the meta class. When we pull this out we create a validation on screen so we do not need to do this in mark-up.
var r1 = new RegularExpressionValidator
ControlToValidate = control.UniqueID,
Display = ValidatorDisplay.None,
ErrorMessage = "The '" + GetDisplayName(e.Entity, e.Field) + "' is not the correct format!",
ValidationExpression = GetRegularExpression(e.Entity, e.Field)
I know this is not a complete description but this should allow you to pull the validation expression out of an entity.