加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将十进制数据类型存储在Azure表中

发布时间:2020-12-15 04:35:28 所属栏目:百科 来源:网络整理
导读:Windows Azure Table Storage does not support是十进制数据类型. A suggested workaround是使用自定义属性将十进制属性序列化为字符串: [EntityDataType(PrimitiveTypeKind.String)]public decimal Quantity { get; set; } 如何实现此EntityDataType自定义
Windows Azure Table Storage does not support是十进制数据类型.

A suggested workaround是使用自定义属性将十进制属性序列化为字符串:

[EntityDataType(PrimitiveTypeKind.String)]
public decimal Quantity { get; set; }

如何实现此EntityDataType自定义属性,从而可以从Windows Azure Tables存储和检索十进制属性?

解决方法

您可以覆盖TableEntity中的WriteEntity方法,并使用EntityResolver
public class CustomTableEntity : TableEntity
{
    private const string DecimalPrefix = "D_";

    public override IDictionary<string,EntityProperty> WriteEntity(OperationContext operationContext)
    {
        var entityProperties = base.WriteEntity(operationContext);
        var objectProperties = GetType().GetProperties();

        foreach (var item in objectProperties.Where(f => f.PropertyType == typeof (decimal)))
        {
            entityProperties.Add(DecimalPrefix + item.Name,new EntityProperty(item.GetValue(this,null).ToString()));
        }

        return entityProperties;
    }
}

我们将使用的实体

public class MyEntity : CustomTableEntity
{
    public string MyProperty { get; set; }

    public decimal MyDecimalProperty1 { get; set; }
    public decimal MyDecimalProperty2 { get; set; }
}

用法包括创建表/插入/检索

#region connection

CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference("mytable");
table.CreateIfNotExists();

#endregion


const string decimalPrefix = "D_";

const string partitionKey = "BlaBlaBla";
string rowKey = DateTime.Now.ToString("yyyyMMddHHmmss");


#region Insert

var entity = new MyEntity
    {
        PartitionKey = partitionKey,RowKey = rowKey,MyProperty = "Test",MyDecimalProperty1 = (decimal) 1.2,MyDecimalProperty2 = (decimal) 3.45
    };

TableOperation insertOperation = TableOperation.Insert(entity);
table.Execute(insertOperation);

#endregion



#region Retrieve

EntityResolver<MyEntity> myEntityResolver = (pk,rk,ts,props,etag) =>
    {
        var resolvedEntity = new MyEntity {PartitionKey = pk,RowKey = rk,Timestamp = ts,ETag = etag};

        foreach (var item in props.Where(p => p.Key.StartsWith(decimalPrefix)))
        {
            string realPropertyName = item.Key.Substring(decimalPrefix.Length);
            System.Reflection.PropertyInfo propertyInfo = resolvedEntity.GetType().GetProperty(realPropertyName);
            propertyInfo.SetValue(resolvedEntity,Convert.ChangeType(item.Value.StringValue,propertyInfo.PropertyType),null);

        }

        resolvedEntity.ReadEntity(props,null);

        return resolvedEntity;
    };

TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey,rowKey,myEntityResolver);
TableResult retrievedResult = table.Execute(retrieveOperation);
var myRetrievedEntity = retrievedResult.Result as MyEntity;

// myRetrievedEntity.Dump(); 

#endregion

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读