title: Data validation with custom attributes (AttributeTargets.Class) on EF buddy classes
body: <p>I have an Entity Framework generated class with the following properties:</p>
  
  <pre><code>public DateTime LaunchDate;
  public DateTime ExpirationDate;
  </code></pre>
  
  <p>I need to enforce that ExpirationDate > LaunchDate.</p>
  
  <p>I am using a buddy class as described in various posts.
  I am applying custom validation attributes (on properties) to other properties in the same buddy class and these are working.</p>
  
  <p>Since I need to compare two properties I am using an attribute directly on the class (AttributeTargets.Class)</p>
  
  <p>Here is my custom validation attribute:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
  public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
  {
      private const string _defaultErrorMessage = "'{0}' must be greater than '{1}'!";
  
      public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
          : base(_defaultErrorMessage)
      {
          GreaterThan = greaterThan;
          Property = property;
      }
  
      public string Property { get; private set; }
      public string GreaterThan { get; private set; }
  
      public override string FormatErrorMessage(string name)
      {
          return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
              GreaterThan, Property);
      }
  
      public override bool IsValid(object value)
      {
          PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
          IComparable greaterThan = properties.Find(GreaterThan, true /* ignoreCase */).GetValue(value) as IComparable;
          IComparable property = properties.Find(Property, true /* ignoreCase */).GetValue(value) as IComparable;
          return greaterThan.CompareTo(property) &gt; 0;
      }
  }
  </code></pre>
  
  <p>First I am unsure to which class I need to apply the attribute to:</p>
  
  <pre><code>[MetadataType(typeof(PromotionValidation))]
  [PropertyMustBeGreaterThanAttribute("RetailPrice")] // apply it here
  public partial class Promotion
  {
      [PropertyMustBeGreaterThanAttribute("ExpirationDate", "LaunchDate")] // or here ???
      public class PromotionValidation
      {
  </code></pre>
  
  <p>Second, it's not working and I have no clue why!!!
  I've tried adding attribute to both classes. Breakpoints in the constructor are hit (PropertyMustBeGreaterThanAttribute ), but IsValid is never called.</p>
  
  <p>Pulling my hair out already....</p>
  
  <p>Thanks!</p>