title: Data Annotations [Required] attribute on property of split Entity Data Model
body: <p>I'm using the <em>Table Splitting</em> feature of the Entity Framework to split my Entity Data Model as follows:</p>
  
  <pre><code>+--------+    +--------------+
  |  News  |    |  NewsImages  |
  +--------+    +--------------+
  | NewsID |    | NewsID       |
  | Text   |    | Image        |
  +--------+    +--------------+
  </code></pre>
  
  <p>Each <code>News</code> entity contains a navigation property called <code>NewsImage</code> to reference the corresponding image.</p>
  
  <hr>
  
  <p>I'm using <em>DataAnnotations</em> to validate my Model. I put the <code>[Required]</code> attribute on the <code>Text</code> property of the <code>News</code> class:</p>
  
  <pre><code>[MetadataType(typeof(NewsValidation))]
  public partial class News
  {
      /* ... */
  }
  
  public class NewsValidation
  {
      [Required]
      public string Text { get; set; }
  }
  </code></pre>
  
  <hr>
  
  <p>Here is the server-side code I use to obtain the image data:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(News news)
  {
      if (ModelState.IsValid)
      {
          UpdateNewsImage(news);
          _newsRepository.Add(news);
          _newsRepository.SaveChanges();
  
          return RedirectToAction("Index");
      }
  }
  
  private void UpdateNewsImage(News news)
  {
      byte[] newsImage = GetNewsImage();
      news.NewsImage = new NewsImage { Image = newsImage };
  }
  
  private byte[] GetNewsImage()
  {
      foreach (string upload in Request.Files)
      {
          HttpPostedFileBase uploadedFile = Request.Files[upload];
  
          if (!uploadedFile.HasFile())
          {
              break;
          }
  
          Stream fileStream = uploadedFile.InputStream;
          int fileLength = uploadedFile.ContentLength;
          byte[] fileData = new byte[fileLength];
          fileStream.Read(fileData, 0, fileLength);
  
          return fileData;
      }
  
      return null;
  }
  </code></pre>
  
  <p>After calling the <code>UpdateNewsImage(news)</code> method, the entity <code>news</code> is <strong>populated correctly</strong> with the corresponding image data but the <code>ModelState.IsValid</code> property is still <code>false</code>; debugging <code>ModelState.Values</code> results in one error: <em>"The NewsImage field is required."</em>.</p>
  
  <hr>
  
  <p>How can I put the <code>[Required]</code> attribute (or some other mechanism enforcing an image for each <code>News</code> entity) on the <code>NewsImage</code> property?</p>