title: Entity Framework: how do I prevent an attached object from getting validation errors on properties not updated?
body: <p>I am trying to attach an object that exists in the database to new DbContext, modify a single value (property: SubmissionGrpID, which is a foreign key) and then save the changes to the database. I am trying to do this without having to load the whole entity into the DbContext from the database.</p>
  
  <p>I have already seen the question: <a href="https://stackoverflow.com/questions/3642371/how-to-update-only-one-field-using-entity-framework">How to update only one field using Entity Framework?</a> but the answers did not help me.</p>
  
  <p>The property: ID in the PnetProdFile table is the only column used in the primary key for that table. The code I am using is below:</p>
  
  <pre><code>using (SomeEntities db = new SomeEntities())
  {
      foreach (var id in FileIds)
      {
          PnetProdFile file = new PnetProdFile();
          file.ID = id; // setting the primary key
          file.SubmissionGrpID = 5; // setting a foreign key
  
          db.PnetProdFiles.Attach(file);
          var entry = db.Entry(file);
          entry.Property(e =&gt; e.SubmissionGrpID).IsModified = true;
      }
      db.SaveChanges();
  }
  </code></pre>
  
  <p>When I run the code, I get the following exception:</p>
  
  <pre><code>Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  </code></pre>
  
  <p>Then, after adding in the following line of code before I SaveChanges():</p>
  
  <pre><code>var errors = db.GetValidationErrors();
  </code></pre>
  
  <p>I get all the Not Null fields as required values for the PnetProdFile table in the Validation Errors. One way to prevent validation errors is to set</p>
  
  <pre><code>db.Configuration.ValidateOnSaveEnabled = false;
  </code></pre>
  
  <p>but then that prevents validation on the entire entity, even on properties that could have been updated.</p>
  
  <p>So, to summarise, I am trying to update an object which already exists in the database without loading the related record from the database, and only get validation errors on properties that have been changed. How can this be done?</p>