空的 PrimitiveList 被 DynamoDB 忽略

Omu*_*Omu 1 .net amazon amazon-web-services amazon-dynamodb

我在 DynamoDB 中保存用户配置文件,并使用属性将 .net 类型转换为 dynamo 条目

所以这是我正在使用的转换器:

public class DictRoomClassHouseInfoConverter : IPropertyConverter
{
    ...

    public DynamoDBEntry ToEntry(object value)
    {
        var dictionary = value as Dictionary<RoomClass, HouseInfo>;

        if (dictionary == null)
        {
            throw new ArgumentException("Invalid type");
        }

        var entry = new PrimitiveList();

        foreach (var kvp in dictionary)
        {
            entry.Add(new Primitive { Value = string.Format("{0}|{1}|{2}", (int)kvp.Key, kvp.Value.House, kvp.Value.TimesSold) });
        }

        return entry;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,当 Dictionary 为空并且PrimitiveList由 返回空时ToEntry,将不会保存此 Dictionary 的更改

所以如果我做这样的事情:

var profile = GetProfile(id);
//profile.DictProp has 3 items; 


profile.DictProp.Clear();
//profile.DictProp has 0 items;

SaveProfile(profile);

var p = GetProfile(id);
//profile.DictProp has 3 items; 
Run Code Online (Sandbox Code Playgroud)

Pav*_*nov 6

@jtlebi 正确回答该服务不接受空字符串或空集。.NET SDK 以此为基础,如果您尝试保存一个空的 PrimitiveList,SDK 将忽略这一点并且属性不会更改。

但是,仍然有一种方法可以删除属性:将 .NET 对象上的字段设置为 null 或从 IPropertyConverter.ToEntry 方法返回 null。然后,当您调用 DynamoDBContext.Save 时,SDK 将为该特定属性发出 DELETE 命令。