How do you exclude a property from being persisted in Azure Table storage?
Asked Answered
S

6

29

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?

Submarine answered 16/2, 2010 at 23:39 Comment(1)
They do now #5379893Cnidus
D
43

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; }
}
Durno answered 13/3, 2016 at 23:28 Comment(0)
H
9

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.

Houdon answered 15/7, 2013 at 13:31 Comment(1)
This has since been superseded by the IgnorePropertyAttribute, see IgnorePropertyAttribute Class.Herrington
S
4

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

Submarine answered 18/2, 2010 at 15:53 Comment(1)
Sadly the links to the forum do not work anymore :-( MSDN really screwed up its links!Piquet
S
4

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]

Striate answered 18/4, 2023 at 14:45 Comment(0)
G
3

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; }
}
Griffey answered 19/4, 2013 at 11:14 Comment(0)
C
0

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://mcmap.net/q/501593/-prevent-azure-tableentity-property-from-being-serialized-in-mvc-4-webapi

Cellar answered 20/9, 2021 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.