【问题标题】:How do you exclude a property from being persisted in Azure Table storage?如何将属性排除在 Azure 表存储中?
【发布时间】:2011-01-17 15:32:48
【问题描述】:

如果我有这样的课程:

    public class Facet : TableServiceEntity
{
    public Guid ParentId { get; set; }      
    public string Name { get; set; }
    public string Uri{ get; set; }
    public Facet Parent { get; set; }
}

Parent 派生自 ParentId Guid,并且该关系旨在由我的存储库填充。那么我如何告诉 Azure 不理会该字段呢?是否存在某种类型的 Ignore 属性,还是我必须创建一个提供这些关系的继承类?

【问题讨论】:

标签: azure azure-storage azure-table-storage


【解决方案1】:

使用最新的Microsoft.WindowsAzure.Storage SDK(v6.2.0 及以上),属性名称已更改为IgnorePropertyAttribute

public class MyEntity : TableEntity
{
     public string MyProperty { get; set; }

     [IgnoreProperty]
     public string MyIgnoredProperty { get; set; }
}

【讨论】:

    【解决方案2】:

    可以在要排除的属性上设置一个名为 WindowsAzure.Table.Attributes.IgnoreAttribute 的属性。只需使用:

    [Ignore]
    public string MyProperty { get; set; }
    

    它是 Windows Azure 存储扩展的一部分,您可以从以下网址下载: https://github.com/dtretyakov/WindowsAzure

    或作为包安装: https://www.nuget.org/packages/WindowsAzure.StorageExtensions/

    该库已获得 MIT 许可。

    【讨论】:

    【解决方案3】:

    来自 bwc 的 Andy Cross 的回复 --- 再次感谢 Andy。 This question an azure forums

    嗨,

    使用 WritingEntity 和 ReadingEntity 事件。 http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.writingentity.aspx 这为您提供了所需的所有控制权。

    这里也有一篇博文作为参考链接:http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/d9144bb5-d8bb-4e42-a478-58addebfc3c8

    谢谢 安迪

    【讨论】:

    • 遗憾的是,论坛的链接不再起作用 :-( MSDN 真的搞砸了它的链接!
    【解决方案4】:

    您可以覆盖 TableEntity 中的 WriteEntity 方法并删除任何具有您的自定义属性的属性。

    public class CustomTableEntity : TableEntity
    {
        public override IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
        {
            var entityProperties = base.WriteEntity(operationContext);
            var objectProperties = GetType().GetProperties();
    
            foreach (var property in from property in objectProperties 
                                     let nonSerializedAttributes = property.GetCustomAttributes(typeof(NonSerializedOnAzureAttribute), false) 
                                     where nonSerializedAttributes.Length > 0 
                                     select property)
            {
                entityProperties.Remove(property.Name);
            }
    
            return entityProperties;
        }
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public class NonSerializedOnAzureAttribute : Attribute
    {
    }
    

    用法

    public class MyEntity : CustomTableEntity
    {
         public string MyProperty { get; set; }
    
         [NonSerializedOnAzure]
         public string MyIgnoredProperty { get; set; }
    }
    

    【讨论】:

      【解决方案5】:

      您还可以将 getter 和 setter 设为非公开,以跳过将属性保存在表存储数据库中。

      见:https://stackoverflow.com/a/21071796/5714633

      【讨论】:

        猜你喜欢
        • 2019-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        • 2012-08-06
        • 1970-01-01
        • 2017-05-17
        • 1970-01-01
        相关资源
        最近更新 更多