Failed to save data with EF6. Error: OriginalValues ​​cannot be used for objects in the added state

I am completely new to entity structure. As a starter to learn more about EF, I am trying to make a generic EF6 implementation with the following example http://genericunitofworkandrepositories.codeplex.com/ . I was able to save data using the same object while trying a simple and simple implementation when I started building the project. But now I got an error when I tried to save the data. Mistake:

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

The message was: OriginalValues ​​cannot be used for objects 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 this on github. Can anyone help me solve this problem? I've been stuck here since yesterday :). I've seen a similar post on Stack Overflow. But they got a problem, for example when a null is passed where there is no null can be accepted in db. In my case, this is not a problem. Please check my repository and suggest what I can do. Any help would be appreciated. here is the lib link: https://github.com/tazbir/TryLib

Edit:

The place of the 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;

                default:
                    return EntityState.Unchanged;
            }
        }
    }

      

+3


source to share


1 answer


I solved the problem by removing the overridden SaveChanges method from the base class. Though I'll have to investigate why removing the SaveChanges () method solved the problem.

here is the code i removed from my context class to get it working.

public override int SaveChanges()
        {
            SyncObjectsStatePreCommit();
            var changes = base.SaveChanges();
            SyncObjectsStatePostCommit();
            return changes;
        }

      



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

care

0


source







All Articles