Azure表存储 - 具有不同名称的TableEntity映射列

时间:2015-05-11 09:08:56

标签: azure mapping azure-storage azure-table-storage

我使用Azure表存储作为我的语义记录应用程序块的数据接收器。当我通过自定义EventSource调用日志时,我会得到类似于ff的列:

  • EVENTID
  • Payload_username
  • 操作码

我可以通过创建一个与列名完全匹配的TableEntity类来获取这些列(出于某种原因除EventId之外):

public class ReportLogEntity : TableEntity
{
    public string EventId { get; set; }
    public string Payload_username { get; set; }
    public string Opcode { get; set; }
}

但是,我想将这些列中的数据存储在TableEntity的不同命名属性中:

public class ReportLogEntity : TableEntity
{
    public string Id { get; set; } // maps to "EventId"
    public string Username { get; set; } // maps to "Payload_username"
    public string Operation { get; set; } // maps to "Opcode"
}

我是否可以使用mapper / attribute来允许自己使列名与TableEntity属性名不同?

1 个答案:

答案 0 :(得分:6)

您可以覆盖接口ReadEntityWriteEntityITableEntity方法来自定义您自己的属性名称。

    public class ReportLogEntity : TableEntity
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string Id { get; set; } // maps to "EventId"
        public string Username { get; set; } // maps to "Payload_username"
        public string Operation { get; set; } // maps to "Opcode"

        public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
        {
            this.PartitionKey = properties["PartitionKey"].StringValue;
            this.RowKey = properties["RowKey"].StringValue;
            this.Id = properties["EventId"].StringValue;
            this.Username = properties["Payload_username"].StringValue;
            this.Operation = properties["Opcode"].StringValue;
        }

        public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
        {
            var properties = new Dictionary<string, EntityProperty>();
            properties.Add("PartitionKey", new EntityProperty(this.PartitionKey));
            properties.Add("RowKey", new EntityProperty(this.RowKey));
            properties.Add("EventId", new EntityProperty(this.Id));
            properties.Add("Payload_username", new EntityProperty(this.Username));
            properties.Add("Opcode", new EntityProperty(this.Operation));
            return properties;
        }
    }