Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Table Storage Entity Row/Primary Key as Attribute for existing properties

I already have entity migrated from EntityFramework. I'm don't want 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; }

It's possible ? I don't want override ReadEntity/WriteEntity.

like image 436
ZOXEXIVO Avatar asked Mar 19 '23 11:03

ZOXEXIVO


2 Answers

Since your class is already based on TableEntity, you might want to try to override/replace the row key and partition key property of TableEntity using the 'new' keyword 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); } }
}
like image 107
Moch Yusup Avatar answered Mar 21 '23 00:03

Moch Yusup


I am not a big fan of 'new' modifier. In my opinion it is non-OOP approach.

I will suggest 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;
        }
    }
}
like image 30
TIKSN Avatar answered Mar 21 '23 00:03

TIKSN