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

问题描述 投票:0回答:2

我使用 Azure 表存储作为语义日志记录应用程序块的数据接收器。当我调用由我的自定义

EventSource
编写的日志时,我得到类似于 ff.:

的列
  • 事件ID
  • 有效负载_用户名
  • 操作码

我可以通过创建一个与列名称完全匹配的

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

我可以使用映射器/属性来让自己的列名称不同于

TableEntity
属性名称吗?

azure mapping azure-storage azure-table-storage
2个回答
10
投票

您可以重写接口 ITableEntityReadEntityWriteEntity 方法来自定义您自己的属性名称。

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

0
投票

您可以使用

DataMember
属性来实现此目的:

public class ReportLogEntity : TableEntity
{
    [System.Runtime.Serialization.DataMember(Name = "EventId")]
    public string Id { get; set; }

    [System.Runtime.Serialization.DataMember(Name = "Payload_username")]
    public string Username { get; set; }

    [System.Runtime.Serialization.DataMember(Name = "Opcode")]
    public string Operation { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.