Azure Table Storage: Ignoring a property of a TableEntity when using the Azure.Data.Tables package
Asked Answered
A

2

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; }
}
Anglicanism answered 3/8, 2021 at 9:43 Comment(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?Gingerly
Hello, I added the POCO that I am trying to persist on Table StorageAnglicanism
T
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.

Tilly answered 16/8, 2021 at 21:10 Comment(1)
Would be nice to see REST equivalentGainer
G
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}
            };
        }
    }
}
Gingerly answered 3/8, 2021 at 11:22 Comment(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.Anglicanism

© 2022 - 2024 — McMap. All rights reserved.