asp.net mvc - Unique constraint with data annotation -
I System.ComponentModel.DataAnnotations'm using
namespace to validate sections of your domain
. How can I create a custom attribute to validate the property's specificity regardless of the database (through some interfaces, for example)?
This is what I came up solution to this solution, it is only for a record with a different ID Checks the table, which is the same value for the property as confirmed. It assumes that you will be using LinqToSql, and there is a single ID column on any table on which this type of verification is required. I also want to put a unique barrier on the underlying table in the database. This feature allows me to put a good error message on the form and to associate with the proper property
public category UniqueAttribute:. ValidationAttribute {public celebration & lt; DataContext & gt; GetDataContext {Get; Private set; } Public string IDProperty {get; Private set; } Public string message {get; Private set; } Public unique properties (type datacontaxtype, string IDPorty, string message) {IDProperty = idProperty; Message = message; GetDataContext = () = & gt; (DataContext) Activator.CreateInstance (dataContextType); } Public unique properties (type datacontact type, string idProperty, string message, string connection string) {IDProperty = idProperty; Message = message; GetDataContext = () = & gt; (Datacontext) activator. Credit instances (data content type, new object [] (connectionString)); } Protected override ValidationResult if (object value, ValidationContext validationContext) {var idProperty = validationContext.ObjectType.GetProperty (IDProperty); Var idType = idProperty.PropertyType; Var id = idProperty.GetValue (validation consultation object instance, empty); P.GetCustomAttributes (false) .offType & lt; DisplayAttribute & gt; () Any (a = & gt. Being validationContext.MemberName null :( God MemberName = validationContext.ObjectType.GetProperties () .Where (p = // ugly hack away gt; a.Name == validationContext. DisplayName)) .Select (p = & gt; p.Name) .FirstOrDefault (); if (string.IsNullOrEmpty (MemberName)) {MemberName = validationContext.DisplayName;} // Hack bridesmaid end validateeProperty = Vadhikrnconteks. Objekttaip. Getproperti (username); revenge Varaitita type = Valiatted type property. property type; Variatted valid = validate property. gate value (validation Contact Object Instens, blank); var Aideepametr = expression. Constant (ID, Aiditiip); revenge Vailetiteperamet = expression. Constant Vadtai, type validity); Var object parameter = expression.pameter (validation contact object type, "o"); Var objectIIDproperty = expression Property (Object Pirator, Idporpati); Var objectValidateeProperty = expression. Property (Object Pirator, Vanenet Property); Var idCheck = expression Not objectIDProperty, idParameter; Var validatee check = expression. Excel (objectValidateeProperty, validateeParameter); Var Composite Test = expression. And (idCheck, validatee check); Var Lambda = Expression Lambda (composite probe, object parameter); . On countMethod = typeof (Queryable) .GetMethods () Single (M = & gt; m.Name == "Calculation" & amp; amp; M.GetParameters () Length == 2); Var generic calculation method = calculation method Mechanical method (validity type type); In the form of IQueryable (var context = GetDataContext ()) using {var table = context.GetTable (validationContext.ObjectType) & lt; Models.Group & gt; Var count = (int) Generic calculation method. Invoke (empty, new object [] {table, lambda}); If (count> 0) {return new verification return (message); }} Return tap; Example}: [MetadataType (typeof (UserMetadata)]] Public partial group group: idatabaseRecord {public class UserMetadata {[required (error message) = "Name is required")] [StringLength (255, ErrorCode = "name must be less than 255 characters")] [Unique (typeF (MyDataContext), "Group", "Name must be unique")] Public string name { Get; Set; }}}