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?

Thomas
  • 24,234
  • 6
  • 81
  • 125
Jeff D
  • 2,164
  • 2
  • 24
  • 39
  • They do now http://stackoverflow.com/questions/5379393/do-azure-table-services-entities-have-an-equivalent-of-nonserializedattribute – Ohad Schneider Sep 09 '14 at 21:32

6 Answers6

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; }
}
kitsu.eb
  • 2,996
  • 1
  • 26
  • 28
Thomas
  • 24,234
  • 6
  • 81
  • 125
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.

patryk
  • 2,747
  • 1
  • 18
  • 8
Mandoleen
  • 2,591
  • 2
  • 20
  • 17
  • 5
    This has since been superseded by the `IgnorePropertyAttribute`, see [IgnorePropertyAttribute Class](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.ignorepropertyattribute.aspx). – Aaron Apr 05 '15 at 08:48
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

Jeff D
  • 2,164
  • 2
  • 24
  • 39
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; }
}
cilerler
  • 9,010
  • 10
  • 56
  • 91
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://stackoverflow.com/a/21071796/5714633

0

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]

tonycdp
  • 137
  • 2
  • 9