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

mig*_*lla 6 mapping azure 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; }
}
Run Code Online (Sandbox Code Playgroud)

但是,我想将这些列中的数据存储在我的不同命名属性中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"
}
Run Code Online (Sandbox Code Playgroud)

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

Zha*_*oft 7

您可以覆盖接口ITableEntity的ReadEntityWriteEntity方法来自定义您自己的属性名称.

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)