EF error: OriginalValues cannot be used for entities in the Added state

August 19, 2013 at 5:23 PMMadestro

I was working on a repository and came across an interesting, cryptic error that resulted from a fairly simple operation: Saving the changes.

Without getting into code details (which are  irrelevant to the point), the call to the following statement failed:


The error message read: OriginalValues cannot be used for entities in the Added state

The issue turned out to be that I had a field of type DateTime on SQL server that was set to NOT NULL. I forgot to assign the value to the entity field before calling SaveChanges() which resulted in a default DateTime value on the entity that was being passed on to SQL. This value was outside the range of valid SQL date values. 

Once I set the value on the field, the call to SaveChanges() completed without a problem.

I have been doing some more research on the subject and my understanding so far is that this situation can also be encountered if you have fields with default values on SQL Server. Because EF does not know about these default values, even if they exist, EF will force the value from the entity (like in my case), including situations where you have a value of NULL.

If anyone else can shed more light on the subject, please feel free to comment.


Posted in: Entity Framework