Click here to Skip to main content
15,991,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

The ingredients to my question.
1 I use a .NET Framework 4.7.2 ASPX website
2 that connects to a library that holds the EFDatabase context to an OData web api.
3. In my webpage I use a GridView component with ItemType, SelectMethod and UpdateMethod.

When the UpdateMethod is executed I do get the correct entity.
When I call the CreateOrUpdate method in my library I see that trying to do context.UpdateObject() an error is given: The context is not currently tracking the entity.

I suspect the disconnected state of the asp.net website makes the context disappear. But in the library the context is available in the class.
In the library the context is not null but
context.AttachTo(entitySetName, entity);
tells me The context is already tracking a different entity with the same resource Uri.

How to overcome this issue?

What I have tried:

C#
public virtual bool CreateOrUpdate(DataServiceContext context, T entity)
        {
            bool result;
            try
            {
                SetupEntityValues(context, entity);

                context.SaveChanges();
                result = true;
            }
            catch (ArgumentNullException argEx)
            {
                Log.Instance.AddException(argEx);
                result = false;
                LastException = argEx;
                if (entity != null)
                    context.Detach(entity);
            }
            catch (InvalidOperationException invOpEx)
            {
                Log.Instance.AddException(invOpEx);
                result = false;
                LastException = invOpEx;
                if (entity != null)
                    context.Detach(entity);
            }

            catch (Exception err)
            {
                Log.Instance.AddException(err);
                if (entity != null)
                    context.Detach(entity);
                result = false;
                LastException = err;
            }
            return result;

        }

        private void SetupEntityValues(DataServiceContext context, T entity)
        {

            if (entity == null)
                throw new ArgumentNullException("No entity given");

            if (context == null)
                throw new ArgumentNullException("no dbContext given");
            var entitySetName = DataServiceContextHelper.GetEntitySetName<T>(context);

            var idProperty = entity.GetType().GetProperty("Id") ?? throw new InvalidOperationException("Entity does not have an Id property.");
            var id = idProperty.GetValue(entity);
            Log.Instance.AddMessage($"Entity name is {entity.GetType().Name}. Id Value = {id}");

            if (id.Equals(0))
            {
                context.AddObject(entitySetName, entity);
            }
            else
            {
                try
                {
                    DetachEntitiesWithSameUri(context, entity);
                    if (!context.IsEntityTracked(entity))
                    {
                        context.AttachTo(entitySetName, entity);
                        context.AddObject(entitySetName, entity);
                    }
                    //else
                    //{
                    //    context.AttachEntity(entity, entitySetName);
                    //}
                }
                catch (Exception err)
                {
                    Log.Instance.AddWarning($"Failed to AttachTo {entitySetName} with Id {id}");
                    Log.Instance.AddException(err);
                }
                context.UpdateObject(entity);
            }
        }
Posted
Updated 13-Jun-24 2:35am
v2

1 solution

Quote:
C#
if (!context.IsEntityTracked(entity))
{
    context.AttachTo(entitySetName, entity);
    context.AddObject(entitySetName, entity);
}
I suspect this line is the problem; you first attach the object in the Unchanged state, and then try to add it in the Added state. It's the AddObject call that's failing.

I suspect the second line should call ChangeObjectState rather than AddObject.

Attaching and Detaching Objects | Microsoft Learn[^]
 
Share this answer
 
Comments
Herman<T>.Instance 13-Jun-24 9:07am    
And now the AttachTo gives the error: The context is already tracking a different entity with the same resource Uri.'
How to prevent that?
Richard Deeming 13-Jun-24 9:14am    
Have you overridden GetHashCode and Equals on your entity class? It looks like that may cause this error message:
Exception if the entity has Equals and GetHashcode implementation · Issue #1531 · OData/odata.net · GitHub[^]
Herman<T>.Instance 13-Jun-24 9:52am    
No
Herman<T>.Instance 13-Jun-24 9:54am    
You are correct about context.AddObject(entitySetName, entity);

But how to find out which other context or entity is already being tracked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900