Entity Framework validation with partial updates

I had this issue today, basically Entity Framework always validate all the entity, so if you want to make a partial update using Attach/Entity State way, you can’t do it if you have configured validation on your model and you don’t fill all your entity with good values, but we want to do a partial update without do another query before, and also without filling up all the entity with data I don’t have, so how to do it?

The most easiest and fastest way is to completely disable the validation for your operation, but this is not the ideal way, what about validating the property I’m near to udpate?

using (var db = new NorthwindDbContext())
{
    // disable the validation for this operation
    db.Configuration.ValidateOnSaveEnabled = false;

    var customer = new Customer
    {
        CustomerId = "ALFKI"
    };
    db.Customers.Attach(customer);

    customer.PostalCode = "123456789000";

    // throw sql exception because PostalCode goes over 10 chars maximum
    // String or binary data would be truncated.
    await db.SaveChangesAsync();
}

Thanks to Shimmy I found what I think is the most elegant solution:

protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
    var result = base.ValidateEntity(entityEntry, items);

    var falseErrors = result.ValidationErrors
                            .Where(error =>
                            {
                                if (entityEntry.State != EntityState.Modified) return false;
                                var member = entityEntry.Member(error.PropertyName);
                                var property = member as DbPropertyEntry;
                                if (property != null) return !property.IsModified;
                                return false;
                            });

    foreach (var error in falseErrors.ToArray())
    {
        result.ValidationErrors.Remove(error);
    }

    return result;
}

To the Shimmy sample I’ve added the condition “if (entityEntry.State != EntityState.Modified) return false;” to avoid bypass the validation when adding an entity.

Now if you test the same code as before, you will receive an entity framework exception and no round-trip will be made on sql server:

using (var db = new NorthwindDbContext())
{
    var customer = new Customer
    {
        CustomerId = "ALFKI"
    };
    db.Customers.Attach(customer);

    customer.PostalCode = "123456789000";

    // Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
    // The field PostalCode must be a string or array type with a maximum length of '10'.
    await db.SaveChangesAsync();
}

EF 6.1.3 RTM Available

Here it is the what’s new:

Entity Framework 6.1.3 just contain fixes to high priority issues that have been reported on the 6.1.2 release. The fixes include:

The runtime as usual is available on NuGet.

Tooling download direct links (you only need to install the tooling if you want to create models using the EF Designer, or generate a Code First model from an existing database):

More info directly from ADO.NET Blog

The EF team are also working on another update to EF6 while working on the next major version of EF (Entity Framework 7).