【问题标题】:Azure Table Generics - Operation not supported on ExecuteQueryAzure 表泛型 - ExecuteQuery 不支持操作
【发布时间】:2021-02-17 01:08:20
【问题描述】:

我正在尝试为 Azure Tables 创建一个通用实现。问题是当我使用 ExecuteQuery 函数时,它总是向我返回以下错误:

错误 = 无法计算表达式。不支持操作。 未知错误:0x80070057。

例如,我确实可以为 TableOperation 删除、更新、创建、检索运行 Execute 函数

这是我在项目中创建的类:

基类

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

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

    public string GetTableName() => TableName;
}

然后是它的接口

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

    TableResult Delete(T id);

    IEnumerable<T> GetByExpression(string query);

    IEnumerable<T> GetAll();
}

以及我拥有的表格的类

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

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

public interface IMapping : ITableEntityBase<Mapping>
{
}

至少,我的服务等级

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.
        }
    }

那么映射服务是:

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

方法调用要简单

    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;
        }
    }

我看到this answer 提出了一个问题,但就我而言,我不知道我需要做什么,因为我已经在我的服务类上定义了new()。我哪里搞砸了?

提前致谢:)

【问题讨论】:

  • MappingServicesMain方法中的定义是什么 -> AddSingleton&lt;IMapping, MappingServices&gt;()?

标签: c# azure generics azure-table-storage azure-tablequery


【解决方案1】:

如果不是,请使用包Microsoft.Azure.Cosmos.Table 1.0.8

我正在使用您的代码进行测试,没有发生错误。测试结果如下:

【讨论】:

  • 解决了!我使用的是 1.0.6 版。谢谢!我看到的另一件事可以用作解决方案,即使使用 1.0.6 也是使用以下代码的方法“ExecuteQuerySegmentedAsync”:stackoverflow.com/a/48227035/4215508 事情是我很奇怪同步方法不起作用,我需要它。
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2017-07-15
  • 2020-02-29
相关资源
最近更新 更多