Windows Azure:“已添加具有相同密钥的项目。” Select 抛出异常
我在尝试从 Windows Azure 表存储下的表中选择一行时遇到奇怪的错误。异常“已添加具有相同密钥的项目。”即使我没有插入任何东西,也会被抛出。导致问题的查询如下:
var ids = new HashSet<string>() { id };
var fields = new HashSet<string> {"@all"};
using (var db = new AzureDbFetcher())
{
var result = db.GetPeople(ids, fields, null);
}
public Dictionary<string, Person> GetPeople(HashSet<String> ids, HashSet<String> fields, CollectionOptions options)
{
var result = new Dictionary<string, Person>();
foreach (var id in ids)
{
var p = db.persons.Where(x => x.RowKey == id).SingleOrDefault();
if (p == null)
{
continue;
}
// do something with result
}
}
如您所见,只有 1 个 id,并且错误在循环顶部抛出,并且没有任何修改。
但是,我使用“”作为该特定行的分区键。什么给?
I'm getting a strange error while trying to select a row from a table under Windows Azure Table Storage. The exception "An item with the same key has already been added." is being thrown even though I'm not inserting anything. The query that is causing the problem is as follows:
var ids = new HashSet<string>() { id };
var fields = new HashSet<string> {"@all"};
using (var db = new AzureDbFetcher())
{
var result = db.GetPeople(ids, fields, null);
}
public Dictionary<string, Person> GetPeople(HashSet<String> ids, HashSet<String> fields, CollectionOptions options)
{
var result = new Dictionary<string, Person>();
foreach (var id in ids)
{
var p = db.persons.Where(x => x.RowKey == id).SingleOrDefault();
if (p == null)
{
continue;
}
// do something with result
}
}
As you can see, there's only 1 id and the error is thrown right at the top of the loop and nothing is being modified.
However, I'm using "" as the Partition Key for this particular row. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在执行此查询之前,您可能已将具有相同行键(且没有分区键)的对象添加到 DataServiceContext 中。然后,您从数据存储中检索冲突的对象,并且由于冲突而无法将其添加到上下文中。
上下文跟踪从表中检索的所有对象。由于实体由其分区键/行键组合唯一标识,因此上下文(如表)不能包含重复的分区键/行键组合。
这种冲突的可能原因是:
在这两种情况下,遇到的上下文已经在跟踪不同的对象,但该对象具有相同的键。这不是上下文可以自行解决的问题,因此是例外。
希望这有帮助。如果您能提供更多信息,那将会很有帮助。
You probably added an object with the same row key (and no partition key) to your DataServiceContext before performing this query. Then you're retrieving the conflicting object from the data store, and it can't be added to the context because of the collision.
The context tracks all object retrieved from the Tables. Since entities are uniquely identified by their partitionKey/rowKey combination, a context, like the tables, cannot contain duplicate partitionkey/rowkey combinations.
Possible causes of such a collison are:
In both cases, the context the encounters it's already tracking a different object which does however have the same keys. This is not something the context can sort out by itself, hence the exception.
Hope this helps. If you could give a little more information, that would be helpful.