locked
Exception: The context is already tracking a different entity with the same resource Uri RRS feed

  • Question

  • I am trying to perform a simple query on Azure Table Storage.  I use the exact same procedure on other tables, but for some reason I'm receiving this exception when querying a specific table.  What seems strange to me is that the query succeeds or fails depending on what data is in the database.  I can typically delete a few rows from the database and the query will succeed.  After my process adds some additional rows I will get this exception again. The entity being stored uses a GUID for the row key that is set whenever an object is instantiated using a non-default constructor.  I have posted the code below.  The exception occurs at the foreach statement.  Before the query is executed however, there are no entities in the context entity dictionary.

    I have scoured the internet for posts about similar problems.  Although others are getting this exception, it appears to be in different circumstances and for different reasons.  Some of the Microsoft documentation I have read talks about needing to handle this exception as it would be expected if the context dictionary already has the entity listed.  Right now I have a blank exception handler, but with this no items ever get processed (in this case added to a list). 

                if (tableClient.DoesTableExist(tableName))
                {
                        string partitionKey = startTime.ToString("MMMyyyy");
    
                        CloudTableQuery<IFSGeoCoordinate> query =
                        (from e in serviceContext.CreateQuery<IFSGeoCoordinate>(tableName)
                         where (e.PartitionKey.Equals(partitionKey)) &&
                         e.CapturedTime >= startTime && e.CapturedTime <= endTime
                         select e).AsTableServiceQuery<IFSGeoCoordinate>();
    
                        IEnumerable<IFSGeoCoordinate> ents = query.Execute();
    
                        try
                        {
    
                            foreach (IFSGeoCoordinate e in ents)
                            {
                                coordinates.Add(e);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }


    • Edited by IFSysDev Tuesday, June 12, 2012 10:56 PM
    Tuesday, June 12, 2012 10:56 PM

Answers

All replies

  • Hi,

    Would you like to share the exception error message or trace message? And does your application works on cloud environment or storage emulator?

    BR,

    Arwind


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Wednesday, June 13, 2012 9:06 AM
  • Arwind, The exception I am receiving is the "Context is already tracking a different entity with the same URI." When I get to work later, I can provide a stack trace if necessary. I am using cloud storage, not the emulator. This occurs even when using a brand new context. In all cases, the Entity dictionary of the context is empty before the query is executed. After execution, that dictionary contains the entities that are expected, and then during the for loop execution before the first entity is processed I receive the mentioned exception. Again, it appears to be highly dependent on what is in the database.
    Wednesday, June 13, 2012 10:42 AM
  • At the moment, the query is only working if I have a single entity in the table storage.  It doesn't matter which one, but it only works if I delete all but one.
    Wednesday, June 13, 2012 2:08 PM
  • Hi,

    According to your description, it seems that you make a query to the entities of table storage, and entities will be tracked. If you add them (tracked) in other collection ("coordinates"), then the error ("Context is already tracking a different entity with the same URI") will be thrown.

    Please refer to below link for more info about table client:

    http://blogs.msdn.com/b/windowsazurestorage/archive/2010/07/26/how-wcf-data-service-changes-in-os-1-4-affects-windows-azure-table-clients.aspx

    Hope this helps.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Wednesday, June 13, 2012 3:00 PM
  • Arwind,

    Thanks for the info, but that doesn't exactly mesh with the experience I'm having.

    For one, I get the exception whether I'm adding it to another collection or just accessing a value, eg

    foreach(IFSGeoCoordinate e in ents)
    {
    	Console.WriteLine(e.Altitude.ToString());
    }

    After digging, it also appears that the problem was caused because the class IFSGeoCoordinate inherited from System.Device.GeoCoordinate.  The subclass had all of the properties (PartitionKey, RowKey, TimeStamp) & decorators necessary to act as a TableServiceEntity.  Nevertheless, it only worked very intermittently.  I removed the inheritance and had it just inherit from TableServiceEntity and everything works fine now.  Not sure exactly what the deal was, but for the moment, I'll have to work around that inheritance.


    Wednesday, June 13, 2012 7:22 PM
  • The reason why this exception is occurring is that your deserialized type does not exactly match the Type of the tracked entity. To avoid this, you can use no tracking option (http://msdn.microsoft.com/en-us/library/system.data.services.client.mergeoption.aspx) 

    or

    use a new dataservicecontext for each logical operation. ( i.e. a context for inserts, then a context for queries etc). 

    joe

    Monday, June 18, 2012 10:25 PM