Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:29:29+00:00 2026-05-31T04:29:29+00:00

I’ve got an azure table record object defined as [DataServiceKey(PartitionKey, RowKey)] public class TableRecord

  • 0

I’ve got an azure table record object defined as

[DataServiceKey("PartitionKey", "RowKey")]
public class TableRecord
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public DateTime Timestamp { get; set; }
    public string Data { get; set; }
}

The record is used as part of the repository infrastructure which accepts business logic level data object and serializes it into the Data property before saving the code to the table storage as well as deserializes it before returning to the client, so the business logic doesn’t know anything about the record as well as about PartitionKey and RowKey.

Here’s the repository method

public TEntity RegisterSave<TEntity>(TEntity entity, bool createNew)
{
    var storeRec = _strategy.GetStoreRecord(entity);
    if (createNew)
        _context.AddObject(storeRec.TableName, storeRec.Record);
    else
    {
        try
        {
            _context.AttachTo(storeRec.TableName, storeRec.Record, "*");                    
        }
        catch (InvalidOperationException)
        {
            // AttachTo can throw an exception if the entity is already being tracked.
            // Ignore
        }

        _context.UpdateObject(storeRec.Record);
    }
    return entity;
}

The _strategy is responsible for correctly mapping the entity type to the table name as well as properly creating TableRecord with keys and serializing the entity into the record. The storeRec.Record property is the instance of the TableRecord class. This approach works well for creating new record and reading record.

But when I try to update existing record with the new data the update fails complaining that it cannot update entity which is not tracked by the context. Although if to step through the code in the debugger it turns out that there are two exceptions actually happening – first in the AttachTo method which complains about the entity with the same key is being tracked, and immediately after that the UpdateObject complains about it’s not.

Where did I go wrong?

Got it

Ok, with a little help from ilspy I’ve found the root cause of the issue. The DataServiceContext maintains two dictionaries for the entities loaded to the context. Keys of one dictionary is entity itself, and key of another one is entity id which is essentially the entity url. In AttachTo method the context verifies both dictionaries and throws InvalidOperationException if entry found in any one of them. But UpdateObject method verifies only dictionary where key is entity itself, and fails if not found.

It appears DataServiceContext assumes that modifications can only be done to the same entity, it doesn’t by default support that the entity will be replaced by the new instance as a whole. But the logic uses standard dictionary class with default comparer so after implementing IEquatable interface for the TableRecord everything worked perfectly.

So for me the solution was:

[DataServiceKey("PartitionKey", "RowKey")]
public class TableRecord: IEquatable<TableRecord>
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public DateTime Timestamp { get; set; }
    public string Data { get; set; }

    public bool Equals(TableRecord other)
    {
        if (other == null)
            return false;
        return PartitionKey.Equals(other.PartitionKey) && RowKey.Equals(other.RowKey);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as TableRecord);
    }

    public override int GetHashCode()
    {
        return PartitionKey.GetHashCode() ^ RowKey.GetHashCode();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:29:31+00:00Added an answer on May 31, 2026 at 4:29 am

    The solution to this is that if there’s an existing entity, you need to Detach() it and the AttachTo() your new one. Then do the updates you want to do.

    I wrote some code that does this. It also avoids throwing an exception, though I don’t know for sure which method is faster.

            /// <summary>
            /// Detach any existing rows with the same keys (if necessary), then attach to this object using the "*" ETag
            /// </summary>
            /// <param name="newEntity"></param>
            protected virtual void SafeAttach(TableServiceEntity newEntity)
            {
                TableServiceEntity entity = GetExistingRow(newEntity.PartitionKey, newEntity.RowKey);
                if(entity != null)
                {
                    base.Detach(entity);
                }
    
                base.AttachTo("MY_TABLE_NAME_GOES_HERE", newEntity, "*");
            }
    
            private TableServiceEntity GetExistingRow(string partitionKey, string rowKey)
            {
                var query = (from e in base.Entities
                             where e.Entity is TableServiceEntity
                             && ((TableServiceEntity)e.Entity).RowKey == rowKey
                             && ((TableServiceEntity)e.Entity).PartitionKey == partitionKey
                             select (TableServiceEntity)e.Entity);
    
                RetrierFunctionResult<TableServiceEntity> r = StorageOperationRetrier.Execute(() =>
                {
                    return query.FirstOrDefault();
                });
    
                return r.Result;
            }
    

    To use this, you would replace your try/catch block with a call to SafeAttach(storeRec).

    Note that this approach defeats the built-in concurrency checking of table storage. You’re basically getting last-write-wins behavior. That may or may not be acceptable, it depends on your situation.

    Also, if this is how you plan to continue working, you may want to set MergeOption.NoTracking. You’re basically emulating that behavior anyway, and there is some performance advantage to disabling entity tracking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.