I have added a row to my database and come back with a different context to update it. I have this class:
public abstract partial class DataManager<I, C>
where C : class, IDomainObject, I, new( ) where I : IDomainObject
C might be an EntityObject, but this class doesn't know that.
My Save looks like this:
public virtual bool Save( I _item )
{
bool rc = true;
try
{
var set = m_Context.GetObjectSet<I, C>( );
ObjectStateEntry stateEntry = null;
if( ! m_Context.ObjectStateManager.TryGetObjectStateEntry( ( C ) _item, out stateEntry ) )
{
if( _item is EntityObject )
{
if ( _item.IsNew )
{
set.AddObject( ( C ) _item );
}
else
{
try
{
set.Attach( ( C ) _item );
}
catch( Exception ex )
{
set.ApplyCurrentValues( ( C ) _item );
}
and so on...
In my test case, stateEntry is not found by TryGetObjectStateEntry. It is, however, an EntityObject, and it is not new (IsNew is my flag), so it gets to the else. Here's my problem: set.Attach throws this error
"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."
and in the very next instant, ApplyCurrentValues throws this one:
"An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied."
How can these both be true?
MORE INFORMATION:
_item was created by a Get in another context. That context was then disposed. At that point, _item had an EntityState.Unchanged. I applied some changes to it, and it changed to EntityState.Modified. (I didn't expect that, since the context (and its ObjectStateManager) should have been gone.) At any rate, once it gets to Save (above), it's state (as reported by the debugger) is Modified, but I have a new context by then. If I get a list of ALL the ObjectStateEntries (Added, Deleted, Modified, Unchanged) at this point, there are only two, and _item is not one of them, as ApplyCurrentValues reports, but it can't be Attached because "it is too there!". Perhaps the problem is that it is still attached to an old ObjectStateManager (could there some reference which won't let the ObjectStateManager dispose?).