在表实体模型的大多数示例中,我看到类似的内容:
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; }
}
我们在此处看到lastname
和firstname
相应地用作分区键和行键。因此,在保存和检索实体之后,我可以从PartitionKey
和RowKey
属性访问这些信息。但是,如果我想稍后将此模型作为json发送到客户端,我认为TableEntity基类的PartitionKey
和RowKey
将不会被序列化。因此,如果我将LastName
和FirstName
添加为模型属性,则会在存储中发生不必要的数据重复。什么是避免存储中的数据重复的最佳方法,同时在序列化模型后可以访问lastname和firstname。
答案 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);