Azure表实体模型设计

时间:2015-10-06 12:05:02

标签: azure database-design model entity

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

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添加为模型属性,则会在存储中发生不必要的数据重复。什么是避免存储中的数据重复的最佳方法,同时在序列化模型后可以访问lastname和firstname。

1 个答案:

答案 0 :(得分:2)

您可以随时在课堂上使用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);