Azure Table Generics - Operation not supported on ExecuteQuery

0

I'm trying to create a generic implementation for Azure Tables. The ploblem is that when I use the ExecuteQuery function it always return the following error to me:

Error = Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.

I do can run the Execute function for TableOperation Delete, Update, Create, Retrieve for example

That's the classes I created on my project:

Base class

public abstract class TableEntityBase : TableEntity
{
    private string TableName { get; set; }

    public TableEntityBase(string tableName)
    {
        TableName = tableName;
    }

    public string GetTableName() => TableName;
}

Then its Interface

public interface ITableEntityBase<T> where T : TableEntityBase
{    
    TableResult InsertOrMerge(T entity);

    TableResult Delete(T id);

    IEnumerable<T> GetByExpression(string query);

    IEnumerable<T> GetAll();
}

And the classes for the tables I have

public class Mapping : TableEntityBase
{
    public Mapping() :
            base(EntityLogicalName)
    {
    }

    private const string EntityLogicalName = "Mapping";
    public string Source { get; set; }
}

public interface IMapping : ITableEntityBase<Mapping>
{
}

At least, my service class

public class TableEntityBaseServices<T> : ITableEntityBase<T> where T : TableEntityBase, new()
{
    protected CloudTable _cloudTable;
    protected string tableName = ((T)Activator.CreateInstance(typeof(T))).GetTableName();

    public TableEntityBaseServices()
    {
        IConfiguration appSettings = AppSettings.GetAppSettings().GetSection("ConnectionStrings");
        _cloudTable = CloudStorageAccountExtensions.CreateCloudTableClient(CloudStorageAccount.Parse(appSettings.GetSection("AzureConfig").Value)).GetTableReference(tableName);
        _cloudTable.CreateIfNotExistsAsync();
    }

//...Other methods that work well

        IEnumerable<T> ITableEntityBase<T>.GetByExpression(string query)
        {
            return _cloudTable.ExecuteQuery<T>(new TableQuery<T>().Where(query)); //Error here: Unable to evaluate the expression. Operation not supported.
        }
    }

The Mapping service then is:

    public class MappingServices : TableEntityBaseServices<Mapping>, IMapping {       }

The method call should be simple

    static async Task Main(string[] args)
    {
        var serviceProvider = new ServiceCollection()
                            .AddSingleton<IMapping, MappingServices>()
                            .BuildServiceProvider();

        IMapping _mappingService = serviceProvider.GetRequiredService<IMapping>();

        try
        {
            IEnumerable<Mapping> mappings  = _mappingService.GetByExpression(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "test1"));
        }
        catch (Exception e)
        {
            throw e;
        }
    }

I saw this answer to a question but in my case I don't know what I need to do, since I already define the new() on my service class. Where did I messed up?

Thanks in advance :)

c#
azure
generics
azure-table-storage
azure-tablequery
asked on Stack Overflow Nov 4, 2020 by Felipe Diniz • edited Nov 13, 2020 by Felipe Diniz

1 Answer

0

Please use the package Microsoft.Azure.Cosmos.Table 1.0.8 if you're not.

I'm testing with your code, no error occurs. The test result as below:

enter image description here

answered on Stack Overflow Nov 16, 2020 by Ivan Yang

User contributions licensed under CC BY-SA 3.0