title: Making AddOrUpdate change only some properties
body: <p>This might be a simple question, however I'm new to Code First and Migrations so bear with me. I'll keep sample code to a minimum to show the problem:</p>
  
  <p>I have a <code>BaseAuditableEntity</code> which includes this (among other things, but let's simplify):</p>
  
  <pre><code>public abstract class BaseAuditableEntity : BaseEntity, IAuditableEntity
  {
    public DateTime CreatedOn { get; set; }
    public DateTime LastModified { get; set; }
  }
  </code></pre>
  
  <p>Now a (for example) <code>User</code> POCO inherits from it:</p>
  
  <pre><code>public class User : BaseAuditableEntity
  {
    public string UserName { get; set; }
    public string PasswordHash { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime? LastLogin { get; set; }
  }
  </code></pre>
  
  <p>I have this on my context's <code>SaveChanges</code> method, to fill in the <code>CreatedOn</code> and <code>LastModified</code> dates (simplified):</p>
  
  <pre><code>public override int SaveChanges()
  {
    var changeSet = ChangeTracker.Entries&lt;IAuditableEntity&gt;();
  
    if (changeSet != null)
    {
      foreach (var entry in changeSet.Where(p =&gt; p.State != EntityState.Unchanged))
      {
        var now = DateTime.UtcNow;
        if (entry.State == EntityState.Added)
          entry.Entity.CreatedOn = now;
        entry.Entity.LastModified = now;
      }
    }      
    return base.SaveChanges();
  }
  </code></pre>
  
  <p>And now I have a migration in place that seeds some users, like this:</p>
  
  <pre><code>protected override void Seed(MyContext context)
  {
    context.Users.AddOrUpdate(
        p =&gt; p.UserName,
        new User { Active = true, 
                   FullName = "My user name",
                   UserName = "ThisUser",
                   PasswordHash = "",
                   Email = "my@email",
                   LastLogin = null,
              }
        // etc.
      );
  }
  </code></pre>
  
  <p>Now I have a problem on seeding with <code>AddOrUpdate</code> after the migration. When the entity is new (it's being added), <code>CreatedOn</code> gets filled correctly and everything works as expected. However when the entity is modified (it already exists on the database and <code>UserName</code> matches), it tries to update it with the new entity I'm creating... this fails because <code>CreatedOn</code> has an invalid <code>DateTime</code> (in this case, <code>DateTime.MinValue</code>).</p>
  
  <p>Is there any way to use the <code>AddOrUpdate</code> method so that it actually retrieves the matching entity from the database and just update the non-default fields? Or maybe some way to tell it which fields NOT to update? For this specific case, I'd like the <code>CreatedOn</code> field to be unchanged, but a generic solution would be appreciated.</p>
  
  <p>Maybe I should do my own <code>AddOrUpdate</code> method which includes a predicate with the fields I want to change, instead of passing it a completely new entity?</p>
  
  <p>This is EF 6.1</p>
  
  <h1>Update</h1>
  
  <p>I know I can easily solve this for the <code>CreatedOn</code> date, this is what I'm currently doing for this specific case:</p>
  
  <pre><code>foreach (var entry in changeSet.Where(c =&gt; c.State != EntityState.Unchanged))
  {
    var now = DateTime.UtcNow;
    if (entry.State == EntityState.Added)
    {
      entry.Entity.CreatedOn = now;
    }
    else
    {
      if (entry.Property(p =&gt; p.CreatedOn).CurrentValue == DateTime.MinValue)
      {
        var original = entry.Property(p =&gt; p.CreatedOn).OriginalValue;
        entry.Property(p =&gt; p.CreatedOn).CurrentValue = original != SqlDateTime.MinValue ? original : now;
        entry.Property(p =&gt; p.CreatedOn).IsModified = true;
       }
     }
     entry.Entity.LastModified = now;
  }
  </code></pre>
  
  <p>I am looking for a more generic solution though</p>