14

这是我的问题。定义订单的类有一个名为 的属性PaymentStatus,它的enum定义如下:

    public enum PaymentStatuses : int
    {
        OnDelivery = 1,
        Paid = 2,
        Processed = 3,
        Cleared = 4
    }

稍后,在类本身中,属性定义非常简单:

    public PaymentStatuses? PaymentStatus { get; set; }

但是,如果我尝试将订单保存到 Azure 表存储,则会收到以下异常:

System.InvalidOperationException: The type Order+PaymentStatuses' has no settable properties.

在这一点上,我认为使用enum是不可能的,但一个快速的谷歌搜索返回了这个:http ://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/7eb1a2ca-6c1b-4440-b40e-012db98ccb0a

此页面列出了两个答案,其中一个似乎忽略了这些问题,并建议enum在 Azure 存储中使用是可以的。

现在,我不需要将 存储enum在 Azure 表存储中,我也可以存储相应的int,但是,我确实需要在 WCF 服务中公开此属性。

我已经尝试使用该属性get并从存储set的 中返回,并通过使用my 上的事件从 Azure 中删除该属性,但是在触发该实体的事件之前我得到了该异常。enumintegerWritingEntityDataContext

在这一点上,我很茫然,我不知道我还能做些什么来将 WCF 中的这个属性enum作为int.

4

6 回答 6

18

不支持枚举。尽管它被定义为一个 int 类型,但它确实不是 Table Storage 支持的整数类型。这是支持的类型列表。枚举只是具有面向对象风格的整数的字符串表达式。

您可以将 int 存储在表存储中,然后使用 Enum.Parse 对其进行转换。

于 2011-05-05T05:53:20.133 回答
12

这是一个简单的解决方法:

public int MyEnumValue { get; set; } //for use by the Azure client libraries only
[IgnoreProperty] public MyEnum MyEnum
{
    get { return (MyEnum) MyEnumValue; }
    set { MyEnumValue = (int) value; }
}

如果可以使用一个简单的支持值而不是一个额外的(公共!)属性,那就更好了——当然,没有覆盖ReadEntity/的麻烦WriteEntity。我打开了一个用户语音票,这将有助于实现这一点,所以你可能想要投票。

于 2015-01-04T21:43:15.553 回答
3

是的,我遇到了同样的问题,我将之前的枚举属性更改为 int。现在这个 int 属性解析传入的 int 并将其保存到相同枚举类型的变量中,所以现在的代码是

public CompilerOutputTypes Type 
{get; set;}

被更改为

private CompilerOutputTypes type;
public int Type 
{
  get {return (int)type;}
  set { type = (CompilerOutputTypes)value; }
}
于 2011-07-14T02:32:27.680 回答
0

Just suggestions...

I remember that in WCF you have to mark enums with special attributes: http://msdn.microsoft.com/en-us/library/aa347875.aspx

Also, when you declare PaymentStatuses? PaymentStatus, you are declaring Nullable<PaymentStatuses> PaymentStatus. The ? sintax is just syntactic sugar. Try to remove the ? and see what happen (you could add a PaymentStatuses.NoSet = 0 , because the default value for an Int32 is 0).

Good luck.

于 2011-02-04T18:49:07.983 回答
0

Parvs 解决方案让我走上了正轨,但我做了一些小的调整。

private string _EnumType;
private EnumType _Type;

//*********************************************
//*********************************************
public string EnumType
{
    get { return _Type.ToString(); }
    set
        {
            _EnumType = value;
            try
            {
                _Type = (EnumType)Enum.Parse(typeof(EnumType), value);     
            }
            catch (Exception)
            {
                _EnumType = "Undefined";
                _Type = [mynamespace].EnumType.Undefined;                  
            }                        
        }
    }
于 2013-06-28T14:57:08.080 回答
0

我遇到了类似的问题,并实现了一个通用的对象扁平化器/重构器 API,它将你的复杂实体扁平化为扁平EntityProperty字典,并使它们可写入表存储,以DynamicTableEntity.

EntityProperty然后,相同的API将从DynamicTableEntity.

这与您的问题相关,因为 ObjectFlatenerRecomposer API 支持展平通常不可写入 Azure 表存储的属性类型,如EnumTimeSpan、所有Nullable类型,ulong并将uint它们转换为可写的 EntityProperties。

EntityPropertyAPI 还处理从扁平化字典转换回原始复杂对象的过程。客户端需要做的就是告诉 API,我有这个EntityProperty字典,我刚刚从 Azure 表中读取(以 DynamicTableEntity.Properties 的形式),你可以将它转换为这个特定类型的对象。API 将重构完整的复杂对象及其所有属性,包括“枚举”属性及其原始正确值。

原始对象的所有这些扁平化和重组都是对客户端(API 的用户)透明地完成的。客户端不需要向 ObjectFlatenerRecomposer API 提供关于它想要编写的复杂对象的任何架构或任何知识,它只需将对象作为“对象”传递给 API 以将其展平。将其转换回来时,客户端只需提供它希望将扁平化EntityProperty字典转换为的对象的实际类型。API 的通用 ConvertBack 方法将简单地重构 Type T 的原始对象并将其返回给客户端。

请参阅下面的使用示例。这些对象也不需要实现任何接口,如“ITableEntity”或从特定的基类继承。它们不需要提供一组特殊的构造函数。

博客: https ://doguarslan.wordpress.com/2016/02/03/writing-complex-objects-to-azure-table-storage/

Nuget 包: https ://www.nuget.org/packages/ObjectFlatenerRecomposer/

用法:

//Flatten object (ie. of type Order) and convert it to EntityProperty Dictionary
 Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(order);

// Create a DynamicTableEntity and set its PK and RK
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);
dynamicTableEntity.Properties = flattenedProperties;

// Write the DynamicTableEntity to Azure Table Storage using client SDK

//Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK
DynamicTableEntity entity = [Read from Azure using the PK and RK];

//Convert the DynamicTableEntity back to original complex object.
 Order order = EntityPropertyConverter.ConvertBack<Order>(entity.Properties);
于 2016-03-11T10:08:00.183 回答