I'm trying to improve the performance when writing/changing data in the database using EntityFramework 6
.
When fetching the entity to be updated I use the following code as I know the primary key from other entities foreign-key relation:
// create a dummy entity
var dummy = new TheEntity
{
TheEntityId = myKey
};
// check if not already attached
var attachedEntity = dbContext.TheEntity.Exists(nrp => nrp.TheEntityId == myKey); // this checks if not already attached
if (attachedEntity == null)
{
// attach to the context
dbContext.TheEntity.Attach(dummy);
}
// return the dummy
return dummy;
Later I change some values of the entity (but only some) which can be different depending on the caller.
However, when SaveChanges
is called I always get System.Data.Entity.Validation.DbEntityValidationException
s for properties I did not touch.
What I want is an update-statement which only updates the values marked as IsModified == true
.
I also checked the entry's properties with dbContext.Entry(dummy).Property(...).IsModified
and only those I have changed are true but the other properties are false
so they should not get updated, no?
What is wrong? As described here it should work as expected.
I tried the answer as well as the comment 'If you don't want to set db.Configuration.ValidateOnSaveEnabled = false
...', but still the same issue. And I really do not want to disable validate.