简体   繁体   中英

404 Error When Deleting Table Row in Azure

When running this code in cloud service project in VS2013, I get 404 error.

How is it possible that I am deleting a row that does not exist while I am retrieving it from the table?

var routeResultQuery = new TableQuery<RouteResult>(); // I deleted where caluse for brevity
var results = resultTable.ExecuteQuery<RouteResult>(routeResultQuery).ToList();

if (results.Count() > 0) // The count shows 26 element when debugging
{
    foreach(RouteResult rr in results)
    {
        var op = TableOperation.Delete(rr);
        resultTable.Execute(op); // The error happens here
    }
}

It turned out that I badly designed the table so that RowKey is overwritten twice when constructed by the retrieving method.

public class RouteResult : TableEntity
{

    public string Session
    {
        get
        {
            return PartitionKey;
        }

        set
        {
            RowKey = value + "-" + DateTime.UtcNow.Ticks; // The issue is here
            PartitionKey = value;
        }
    }
}

The issue is not really in the overwriting operation but in the variable value DateTime.UtcNow.Ticks that result in a different RowKey, thus 404 not found.

I changed the table to:

public class RouteResult : TableEntity
{
    public RouteResult(string sessionId)
    {
        RowKey = sessionId + "-" + Guid.NewGuid();
        PartitionKey = sessionId;
    }

    public RouteResult()
    {

    }
}

And now it is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM