我正在尝试为 Azure 表创建一个通用实现。问题是当我使用 ExecuteQuery 函数时,它总是向我返回以下错误:
错误 = 无法计算表达式。不支持操作。未知错误:0x80070057。
例如,我确实可以为 TableOperation Delete、Update、Create、Retrieve 运行 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;
}
}
我看到了这个问题的答案,但就我而言,我不知道我需要做什么,因为我已经new()
在我的服务类上定义了。我哪里搞砸了?
提前致谢 :)