Robert
Robert

Reputation: 515

Avoid updating non nullable field

When updating a row from a detached entity, I'm trying to avoid updating fields which haven't changed, using this:

DbEntityEntry<Type> entry = context.Entry(entity);
entry.Property(p => p.Property).IsModified = false;

This works fine for nullable fields, but when it's a non nullable field EF throws a validation exception saying that the field is required.

Anyone have any insight into why this doesn't work and if I must fiddle around with original values to overcome it?
I'm using EF 5.0 and the DBContext-API.

Upvotes: 1

Views: 254

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364269

The reason is validation logic inside EF. The validation is always triggered on the whole entity - it doesn't respect modifications. The solution is to disable entity validation logic in EF:

context.Configuration.ValidateOnSaveEnabled = false;

Upvotes: 2

Related Questions