Failed to save data using EF6. Error: OriginalValues cannot be used for entities in the Added state

c# entity-framework entity-framework-6


I am quite new to entity framework. As a starter to understand more about EF, I am trying to make a generic implementation of EF6 following example of . I was able to save data using same entity while tried plain and simple implementation when I started creating the project. But, now i got error while I tried to save data. The Error:

  • OriginalValues '(($ReturnValue1)).OriginalValues' threw an exception of type 'System.InvalidOperationException' System.Data.Entity.Infrastructure.DbPropertyValues {System.InvalidOperationException}

The message was: OriginalValues cannot be used for entities in the Added state.

Stack trace:

   at System.Data.Entity.Internal.InternalEntityEntry.ValidateStateToGetValues(String method, EntityState invalidState)
   at System.Data.Entity.Internal.InternalEntityEntry.get_OriginalValues()
   at System.Data.Entity.Infrastructure.DbEntityEntry`1.get_OriginalValues()

I have it available on github. Can anyone help me resolve this problem? I am stuck here from yesterday :). I saw similar post on stack overflow. But, they got problem like null value passed where there is no null value can be accepted in db. In my case, that is not the problem. Please check my repository and suggest what i can do.. Any help appreciated. here is the lib link:


The place of error is here:

public void SyncObjectState<TEntity>(TEntity entity) where TEntity : class, IObjectState

Entry(entity).State = StateHelper.ConvertState(entity.ObjectState);(error triggers after executing this line)


public class StateHelper
        public static EntityState ConvertState(ObjectState state)
            switch (state)
                case ObjectState.Added:
                    return EntityState.Added;

                case ObjectState.Modified:
                    return EntityState.Modified;

                case ObjectState.Deleted:
                    return EntityState.Deleted;

                    return EntityState.Unchanged;
7/10/2015 7:06:55 AM

Accepted Answer

I resolved the issue by removing overridden method SaveChanges from the base class. Though I will have to investigate why removing SaveChanges() method resolved the issue.

here is the code block that I removed from my context class to get it work.

public override int SaveChanges()
            var changes = base.SaveChanges();
            return changes;

thanks guys... You might not be able to understand the scenario. If anybody wants to play with this, you can check out my repository at github.

take care

7/11/2015 5:09:34 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow