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;
}
}
}