Azure Table 存储 - Table 具有不同名称的实体映射列

Azure Table Storage - TableEntity map column with a different name

我正在使用 Azure Table 存储作为我的语义日志记录应用程序块的数据接收器。当我调用要由我的自定义 EventSource 写入的日志时,我得到类似于 ff.:

的列

我可以通过创建与列名称完全匹配的 TableEntity class 来获取这些列(出于某种原因,[=15= 除外):

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 属性 名称的列名称?

您可以覆盖 ReadEntity and WriteEntity methods of interface ITableEntity 以自定义您自己的 属性 名称。

    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;
        }
    }