如何排除属性保留在 Azure 表存储中?

发布于 2024-08-22 00:04:49 字数 367 浏览 13 评论 0 原文

如果我有一个这样的类:

    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 属性,或者我是否必须创建一个提供这些关系的继承类?

If I have a class like this:

    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 is derived from the ParentId Guid, and that relationship is intended to be filled in by my repository. So how do I tell Azure to leave that field alone? Is there an Ignore attribute of some type, or do I have to create an inherited class that provides those relationships instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉梦枕江山 2024-08-29 00:04:49

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

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

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

Using latest Microsoft.WindowsAzure.Storage SDK (v6.2.0 and up), the attribute name has changed to IgnorePropertyAttribute :

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

     [IgnoreProperty]
     public string MyIgnoredProperty { get; set; }
}
你怎么这么可爱啊 2024-08-29 00:04:49

可以在要排除的属性上设置一个名为 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 许可。

There is an attribute called WindowsAzure.Table.Attributes.IgnoreAttribute can be set on the property you want to exclude. Just use:

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

It is a part of Windows Azure Storage Extensions, which you may download from:
https://github.com/dtretyakov/WindowsAzure

or install as a package:
https://www.nuget.org/packages/WindowsAzure.StorageExtensions/

The library is MIT licensed.

很快妥协 2024-08-29 00:04:49

来自生物武器公约安迪·克罗斯的回复 --- 再次感谢安迪。
此问题是 azure 论坛< /a>

嗨,

使用 WriteEntity 和 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

谢谢
安迪

This reply from Andy Cross at bwc --- Thank you again Andy.
This question an azure forums

Hi,

Use the WritingEntity and ReadingEntity events. http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.writingentity.aspx This gives you all the control you need.

For reference there's a blog post linked off here too: http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/d9144bb5-d8bb-4e42-a478-58addebfc3c8

Thanks
Andy

少女的英雄梦 2024-08-29 00:04:49

这些答案可能有点过时了。

对于那些已登陆此处但仍需要最新版本 Azure 的此功能的用户,请使用 [IgnoreDataMember]

These answers might be a little out of date.

For those of you who have landed here and still needing this functionality with the latest versions of Azure, use [IgnoreDataMember]

生死何惧 2024-08-29 00:04:49

您可以重写 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; }
}

You may override the WriteEntity method in TableEntity and remove any properties that have your custom attribute.

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

usage

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

     [NonSerializedOnAzure]
     public string MyIgnoredProperty { get; set; }
}
肥爪爪 2024-08-29 00:04:49

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

请参阅:https://stackoverflow.com/a/21071796/5714633

You could also make the getter and setter non-public in order to skip the property from being saved in the table storage database.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文