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

  • EventId
  • 有效负载用户名
  • 操作码

  • 我可以通过创建一个与列名完全匹配的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属性名称不同的列名称?

    最佳答案

    您可以重写ReadEntity接口(interface)的WriteEntityITableEntity方法来自定义您自己的属性名称。

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

    09-05 08:02