Azure TableEntity,覆盖 Write 和 ReadEntity 方法

标签 azure azure-table-storage

我有一个 Azure 表,有一些像这样的枚举

pk   rk   en     fr     de   ...

foo  1  'E-f1' 'F-f1' 'D-f1' ...
foo  2  'E-f2' 'F-f2' 'D-f2' ...

bar  1  'E-b1' 'F-b1' 'D-b1' ...
bar  2  'E-b2' 'F-b2' 'D-b2' ...
bar  3  'E-b3' 'F-b3' 'D-b3' ...
en , fr , de等...是语言代码,分别是表中的列名。

当用户选择一种语言时,我需要为“foo”和“bar”选择下拉菜单
我需要显示下拉菜单。

我创建了一个 DescriptionEntity ,有一个 Description应该保留 'en' 或 'fr' 或 'de' 文本值,具体取决于当前的用户界面语言。

以下代码是否替换了 Description属性与 Azure 表中的当前语言值并分别读取它,或者我误解了一些东西(因为文档中不是很清楚)......?
public class DescriptionEntity : TableEntity
{
    public string Description { get; set; }
    private string lang;

    public DescriptionEntity(string lang) {
        this.lang = lang;
    }

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) {
        base.ReadEntity(properties, operationContext);
        if (properties.ContainsKey(this.lang)) {
            this.Description = properties[this.lang].StringValue;
        }
    }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        var x = base.WriteEntity(operationContext);
        var descriptionValue = this.Description;
        if (x.ContainsKey(lang)) {
            x[lang].StringValue = this.Description;
        }
        else {
            x.Add(lang, new EntityProperty(this.Description));
        }
        x.Remove("Description");
        return x;
    }
}

最佳答案

I created a DescriptionEntity, that has a Description that should keep the 'en' or 'fr' or 'de' text value, depending on the current user interface language.

Does the following code replace the Description property with the current language value in the Azure Table and read it respectively, or I misunderstand something (because there is not very clear for the in the documentation)...?


根据我对以下示例的测试,您的代码应该替换 Description具有基于当前用户界面语言的语言值的属性。
我的 Azure 表中的实体:
enter image description here
说明实体类:
public class DescriptionEntity : TableEntity
{
    public DescriptionEntity(string pk, string rk)
    {
        this.PartitionKey = pk;
        this.RowKey = rk;
    }
    public DescriptionEntity() { }

public string Description { get; set; }

    string lang = "en";

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
    {
        base.ReadEntity(properties, operationContext);
        if (properties.ContainsKey(lang))
        {
            this.Description = properties[lang].StringValue;
        }
    }
}
查询实体:
TableQuery<DescriptionEntity> query = new TableQuery<DescriptionEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "foo"));

foreach (DescriptionEntity entity in table.ExecuteQuery(query))
{
    string languages = entity.Description;
}
enter image description here

关于Azure TableEntity,覆盖 Write 和 ReadEntity 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513425/

相关文章:

azure - 如何通过azure使用现有数据?

azure - 查找某个区域可用的核心总数

Azure 通知中心 - 从 GCM 迁移到 FCM

c# - 使现有实体实现 TableEntity

azure - 使用实体组事务清除 WADLogs 表时如何处理 "The specified resource does not exist"异常

azure - 在Azure Blob存储中为大量数据设置Blob索引标记的最佳方法是什么

node.js - 在azure应用程序服务中使用docker-compose

azure - 提高 Azure 表存储批量加载的性能

azure - 关于Azure表设计

node.js - 表实体未保留