13

I am using the new Azure.Data.Tables library from Microsoft to deal with Azure Table Storage. With the old library when you had an entity that implemented ITableEntity and you had a property that you did not want to save to the storage table you would use the [IgnoreProperty] annotation. However, this does not seem to be available on the new library.

What would be the equivalent on the Azure.Data.Tables package or how do you now avoid saving a property to table storage now?

This is the class I want to persist:

public class MySpatialEntity : ITableEntity
{
    public int ObjectId { get; set; }
    public string Name { get; set; }
    public int MonitoringArea { get; set; }

    //This is the property I want to ignore because table storage cannot store it
    public Point Geometry { get; set; }

    //ITableEntity Members
    public virtual string PartitionKey { get => MonitoringArea.ToString(); set => MonitoringArea = int.Parse(value); }
    public virtual string RowKey { get => ObjectId.ToString(); set => ObjectId = int.Parse(value); }
    public DateTimeOffset? Timestamp { get; set; }
    public ETag ETag { get; set; }
}
2
  • Can you edit your question and include the code (including the model) that you are migrating from using old SDK to the new one? Commented Aug 3, 2021 at 9:47
  • Hello, I added the POCO that I am trying to persist on Table Storage Commented Aug 3, 2021 at 10:29

2 Answers 2

34

As of version 12.2.0.beta.1, Azure.Data.Tables table entity models now support ignoring properties during serialization via the [IgnoreDataMember] attribute and renaming properties via the [DataMember(Name="<yourNameHere>")] attribute.

See the changelog here.

1
  • Would be nice to see REST equivalent
    – BorisD
    Commented Jan 30, 2023 at 10:57
1

I don't think there's anything like [IgnoreProperty] available as of now (at least with version 12.1.0).

I found two Github issues which talk about this:

What you can do is create a custom dictionary of the properties you want to persist in the entity and use that dictionary for add/update operations.

Please see sample code below:

using System;
using System.Collections.Generic;
using System.Drawing;
using Azure;
using Azure.Data.Tables;

namespace SO68633776
{
    class Program
    {
        private static string connectionString = "connection-string";
        private static string tableName = "table-name";
        static void Main(string[] args)
        {
            MySpatialEntity mySpatialEntity = new MySpatialEntity()
            {
                ObjectId = 1,
                Name = "Some Value",
                MonitoringArea = 2
            };
            TableEntity entity = new TableEntity(mySpatialEntity.ToDictionary());
            TableClient tableClient = new TableClient(connectionString, tableName);
            var result = tableClient.AddEntity(entity);
        }
    }
    public class MySpatialEntity: ITableEntity
    {
        public int ObjectId { get; set; }
        public string Name { get; set; }
        public int MonitoringArea { get; set; }
        
        //This is the property I want to ignore because table storage cannot store it
        public Point Geometry { get; set; }

        //ITableEntity Members
        public virtual string PartitionKey { get => MonitoringArea.ToString(); set => MonitoringArea = int.Parse(value); }
        public virtual string RowKey { get => ObjectId.ToString(); set => ObjectId = int.Parse(value); }
        public DateTimeOffset? Timestamp { get; set; }
        public ETag ETag { get; set; }

        public IDictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>()
            {
                {"PartitionKey", PartitionKey},
                {"RowKey", RowKey},
                {"ObjectId", ObjectId},
                {"Name", Name},
                {"MonitoringArea", MonitoringArea}
            };
        }
    }
}
1
  • 1
    Thanks for your answer. I'm surprised this feature, which seems very basic is yet not available. I will keep track of issue 19782 on github. Commented Aug 3, 2021 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.