3

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 http://genericunitofworkandrepositories.codeplex.com/ . 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: https://github.com/tazbir/TryLib

Edit:

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;

                default:
                    return EntityState.Unchanged;
            }
        }
    }
2
  • You should post the relevant part of the code here, why would anyone want to to browse your entire source code? Jul 10, 2015 at 6:36
  • thanks for your comment. Please review Jul 10, 2015 at 6:43

1 Answer 1

0

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()
        {
            SyncObjectsStatePreCommit();
            var changes = base.SaveChanges();
            SyncObjectsStatePostCommit();
            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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.