繁体   English   中英

Azure表实体模型设计

[英]Azure table entity model design

在大多数表实体模型的示例中,我看到类似以下内容的内容:

public class CustomerEntity : TableEntity
{
    public CustomerEntity(string lastName, string firstName)
    {
        this.PartitionKey = lastName;
        this.RowKey = firstName;
    }

    public CustomerEntity() { }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }
}

正如我们在这里看到的lastnamefirstname作为相应的分区键和行键。 因此,稍后在保存和检索实体之后,我可以从PartitionKeyRowKey属性访问那些信息。 但是,如果我想稍后将此模型作为json发送到客户端,我认为TableEntity基类的PartitionKeyRowKey不会被序列化。 因此,如果我添加LastNameFirstName作为要建模的属性,则在存储中会发生不必要的数据重复。 避免存储中数据重复的最佳方法是什么,同时在序列化模型后同时访问姓和名。

您可以始终在类上使用getter方法以避免混淆:

public class CustomerEntity : TableEntity
{
    public CustomerEntity(string lastName, string firstName)
    {
        this.PartitionKey = lastName;
        this.RowKey = firstName;
    }

    public CustomerEntity() { }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public string FirstName { get { return this.RowKey; } }

    public string LastName { get { return this.PartitionKey; } }

}

或者,您可以将数据映射到API中的匿名对象,然后通过JSON返回:

var customerJson = new
{
    Firstname = customer.RowKey,
    LastName = customer. PartitionKey,
    customer.Email,
    customer.PhoneNumber
};
return JsonConvert.SerializeObject(customerJson);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM