Azure表存储实体行/主键作为现有属性的属性

时间:2014-09-15 06:58:53

标签: azure azure-storage

我已经从EntityFramework迁移了实体。 我不想覆盖一些propeties并将其转换为字符串

public class User : TableEntity, ITableStorageEntity<int, Guid>
{        
    [RowKey]
    public Guid ID { get; set; }
    [PartitionKey]
    public int LanguageID { get; set; }

有可能吗?我不想覆盖ReadEntity / WriteEntity。

2 个答案:

答案 0 :(得分:3)

由于您的类已经基于TableEntity,您可能希望尝试使用'new'关键字覆盖/替换TableEntity的行键和分区键属性。

public class User : TableEntity
{
    [IgnoreProperty]
    public Guid ID { get; set; }

    [IgnoreProperty]
    public int LanguageID { get; set; }

    public new string PartitionKey { get { return ID.ToString(); } set { ID = Guid.Parse(value); } }

    public new string RowKey { get { return LanguageID.ToString(); } set { LanguageID = int.Parse(value); } }
}

答案 1 :(得分:2)

我不是'新'修饰符的忠实粉丝。在我看来,这是非OOP方法。

我建议关注

public class ConsumerApplicationEntity : TableEntity
{
    public ConsumerApplicationEntity(string applicationKey, string applicationSecret)
        : base(applicationKey, applicationSecret)
    {

    }

    [IgnoreProperty]
    public string ApplicationKey
    {
        get
        {
            return this.PartitionKey;
        }
        set
        {
            this.PartitionKey = value;
        }
    }

    [IgnoreProperty]
    public string ApplicationSecret
    {
        get
        {
            return this.RowKey;
        }
        set
        {
            this.RowKey = value;
        }
    }
}