Azure Table Entity Row / Primary Key as Attribute for Existing Properties

I already have an entity migrated from EntityFramework. I don't want to override some propeties and convert it to string

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

      

Is it possible? I don't want to override ReadEntity / WriteEntity.

+3


source to share


2 answers


Since your class is already based on TableEntity, you can try to override / replace the row key and the section key property of TableEntity using "new" instead.



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

      

+3


source


I'm not a big fan of the "new" modifier. In my opinion this is a different approach from OOP.

I suggest the following



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

      

+1


source







All Articles