Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What is the best way to add attributes to auto-generated entities (using VS2010 and EF4)
  2. List<ValidationResult> errorList = new List<ValidationResult>();
  3.         bool bValid = client.IsValid<Client, ClientMetadata>(ref errorList, false);
  4.  
  5.  
  6.     public static bool IsValid<T, U>(this T obj, ref List<ValidationResult> errors, bool validateAllProperties = true) where T : IValidatableObject
  7.     {
  8.         //If metadata class type has been passed in that's different from the class to be validated, register the association
  9.         if (typeof(T) != typeof(U))
  10.         {
  11.             TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(T), typeof(U)), typeof(T));
  12.         }
  13.  
  14.         var validationContext = new ValidationContext(obj, null, null);
  15.         var validationResults = new List<ValidationResult>();
  16.         Validator.TryValidateObject(obj, validationContext, validationResults, validateAllProperties);
  17.  
  18.         errors = validationResults;
  19.  
  20.         if (validationResults.Count > 0)
  21.             return false;
  22.         else
  23.             return true;
  24.     }