添加或Azure Table中存储取代实体 [英] Add or replace entity in Azure Table Storage

查看:196
本文介绍了添加或Azure Table中存储取代实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows Azure表的存储工作,并有一个简单的要求:添加一个新行,覆盖与PartitionKey / RowKey任何现有行。然而,保存更改总是抛出异常,即使我通过在ReplaceOnUpdate选项:

I'm working with Windows Azure Table Storage and have a simple requirement: add a new row, overwriting any existing row with that PartitionKey/RowKey. However, saving the changes always throws an exception, even if I pass in the ReplaceOnUpdate option:

tableServiceContext.AddObject(TableName, entity);
tableServiceContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);

如果实体已经存在,它抛出:

If the entity already exists it throws:

System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---> System.Data.Services.Client.DataServiceClientException: <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code>EntityAlreadyExists</code>
  <message xml:lang="en-AU">The specified entity already exists.</message>
</error>

难道我真的对现有行手动查询第一,并呼吁 DeleteObject的就可以了?这似乎很慢。当然还有更好的办法?

Do I really have to manually query for the existing row first and call DeleteObject on it? That seems very slow. Surely there is a better way?

推荐答案

正如您看到的,你不能只是添加具有相同的行键和分区键另一个项目,所以你需要运行一个查询检查,看看是否该项目已经存在。在这种情况下,我觉得看 Azure的REST API文档来看看什么是有帮助提供给存储客户机库。你会看到有对不同的方法插入并的updating 。该 ReplaceOnUpdate 只有一个效果,当你udpating,不插入。

As you've found, you can't just add another item that has the same row key and partition key, so you will need to run a query to check to see if the item already exists. In situations like this I find it helpful to look at the Azure REST API documentation to see what is available to the storage client library. You'll see that there are separate methods for inserting and updating. The ReplaceOnUpdate only has an effect when you're udpating, not inserting.

虽然你可以删除现有的项,然后添加新的,你可以只更新现有一(节省您一个往返到存储)。您code可能是这个样子:

While you could delete the existing item and then add the new one, you could just update the existing one (saving you one round trip to storage). Your code might look something like this:

var existsQuery = from e
                    in tableServiceContext.CreateQuery<MyEntity>(TableName)
                    where
                    e.PartitionKey == objectToUpsert.PartitionKey
                    && e.RowKey == objectToUpsert.RowKey
                    select e;

MyEntity existingObject = existsQuery.FirstOrDefault();

if (existingObject == null)
{
    tableServiceContext.AddObject(TableName, objectToUpsert);
}
else
{
    existingObject.Property1 = objectToUpsert.Property1;
    existingObject.Property2 = objectToUpsert.Property2;

    tableServiceContext.UpdateObject(existingObject);
}

tableServiceContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);

编辑:虽然在正确写作的时候,与2011年9月更新微软已经更新了Azure的表API包括两个UPSERT commandes,的插入或更换实体插入或合并实体

While correct at the time of writing, with the September 2011 update Microsoft have updated the Azure table API to include two upsert commandes, Insert or Replace Entity and Insert or Merge Entity

这篇关于添加或Azure Table中存储取代实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆