title: Data Annotations with Entity Framework 5.0 (database first)
body: <p>What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach?</p>
  
  <p>This is my partial class created by Entity Framework:</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //    This code was generated from a template.
  //
  //    Manual changes to this file may cause unexpected behavior in your application.
  //    Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  using System.ComponentModel.DataAnnotations;
  
  namespace ACore
  {
      using System;
      using System.Collections.Generic;
  
      public partial class PayrollMarkup_State
      {
          [UIHint("StatesEditor")] // &lt;-- I added this line but it will be overwritten
          public string State { get; set; }
          public Nullable&lt;float&gt; MaintenancePercentage { get; set; }
          public Nullable&lt;float&gt; OfficePercentage { get; set; }
      }
  }
  </code></pre>
  
  <p>I tried this with no success....</p>
  
  <p>Entity Framework generated file: 'PayrollMarkup_State.cs'</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //    This code was generated from a template.
  //
  //    Manual changes to this file may cause unexpected behavior in your application.
  //    Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  using System.ComponentModel.DataAnnotations;
  
  namespace ACore
  {
      using System;
      using System.Collections.Generic;
  
      public partial class PayrollMarkup_State
      {
          public string State { get; set; }
          public Nullable&lt;float&gt; MaintenancePercentage { get; set; }
          public Nullable&lt;float&gt; OfficePercentage { get; set; }
      }
  }
  </code></pre>
  
  <p>I then created this file in a different directory: 'PayrollMarkup_state.cs'</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace ACore.Models
  {
      [MetadataType(typeof(PayrollMarkupMetadata))]
      public partial class PayrollMarkup_State
      {
      }
  
      public class PayrollMarkupMetadata
      {
          [UIHint("StatesEditor")]
          public string State; // Has to have the same type and name as your model
      }
  }
  </code></pre>