本文介绍了存储在Azure的表十进制数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Windows Azure的表的存储在十进制数据类型。
Windows Azure Table Storage does not support the decimal data type.
A suggested解决方法是使用自定义属性序列化小数属性作为字符串:
A suggested workaround is to use a custom attribute to serialize the decimal property as string:
[EntityDataType(PrimitiveTypeKind.String)]
public decimal Quantity { get; set; }
这怎么可能EntityDataType自定义属性得以落实,小数属性可以被储存和调用Windows Azure的表?
How could this EntityDataType custom attribute be implemented so decimal properties can be stored and retrieved from Windows Azure Tables?
推荐答案
您可能会覆盖WriteEntity方法TableEntity和使用的EntityResolver
You may override the WriteEntity method in TableEntity and use 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;
}
}
实体,我们将使用
the entity we will use
public class MyEntity : CustomTableEntity
{
public string MyProperty { get; set; }
public decimal MyDecimalProperty1 { get; set; }
public decimal MyDecimalProperty2 { get; set; }
}
使用,其中包括创建表格/插入/ Retreive
usage which includes Create Table / Insert / Retreive
#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
这篇关于存储在Azure的表十进制数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!