[英]Windows Azure: “An item with the same key has already been added.” exception thrown on Select
I'm getting a strange error while trying to select a row from a table under Windows Azure Table Storage. 尝试从Windows Azure表存储下的表中选择一行时遇到一个奇怪的错误。 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. 如您所见,只有1个id,并且错误直接在循环的顶部抛出,并且没有任何修改。
However, I'm using "" as the Partition Key for this particular row. 但是,我使用“”作为此特定行的分区键。 What gives? 是什么赋予了?
You probably added an object with the same row key (and no partition key) to your DataServiceContext before performing this query. 在执行此查询之前,您可能已将具有相同行键(但没有分区键)的对象添加到DataServiceContext中。 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. 由于实体由它们的partitionKey / rowKey组合唯一标识,因此上下文(如表)不能包含重复的partitionkey / rowkey组合。
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. 如果您能提供更多信息,那将有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.