简体   繁体   English

从“WindowsAzure.Storage”迁移时,我应该为 Azure 表使用什么 package

[英]What package should I use for Azure Tables when migrating away from "WindowsAzure.Storage"

I am currently migrating a project that was written for .NET Fx 4.8 using ASP.NET WebApi to .NET 5.0.我目前正在将使用 ASP.NET WebApi 为 .NET Fx 4.8 编写的项目迁移到 .NET 5.0。 This project makes heavy use of the package WindowsAzure.Storage for accessing BLOBs, tables and queues on a Azure Storage account.该项目大量使用 package WindowsAzure.Storage来访问 Azure 存储帐户上的 BLOB、表和队列。 This package has been deprecated for a while now, stating that is has been split up into several packages.这个 package 现在已经被弃用了一段时间,说明它已经被分成几个包。

For Queues and Blobs it is quite clear what I should migrate to.对于队列和 Blob,我应该迁移到什么位置非常清楚。 But there seems to be a lot of confusion regarding Azure Tables however.但是,关于 Azure 表似乎有很多混淆。

  • First of all in the NuGet package WindowsAzure.Storage it states that the package to use for tables would be Microsoft.Azure.CosmosDB.Table . First of all in the NuGet package WindowsAzure.Storage it states that the package to use for tables would be Microsoft.Azure.CosmosDB.Table . It's a bit confusing because I always thought that "CosmosDB" is something entirely different than "Azure Tables", but maybe I am wrong.这有点令人困惑,因为我一直认为“CosmosDB”与“Azure Tables”完全不同,但也许我错了。
  • The description of Microsoft.Azure.CosmosDB.Table however states that this package is in maintenance mode and will be deprecated.但是, Microsoft.Azure.CosmosDB.Table的描述指出此 package 处于维护模式,将被弃用。 I should use Microsoft.Azure.Cosmos.Table instead.我应该改用Microsoft.Azure.Cosmos.Table
  • Microsoft.Azure.Cosmos.Table however has not been updated for ages and 2.0.0 is in preview for more than two years now. Microsoft.Azure.Cosmos.Table但是很久没有更新了,而且 2.0.0 的预览版已经两年多了。 There is also an issue on its GitHub repo that specifically asks if this project is dead - with no answer. GitHub repo 上还有一个问题,专门询问该项目是否已死 - 没有答案。
  • After searching around, I found the package Azure.Data.Tables to be closest to what I might be looking for.搜索后,我发现 package Azure.Data.Tables最接近我可能正在寻找的东西。 It's updated, looks alive and seems in line (version-number wise) with the other storage packages.它已更新,看起来很活跃,并且似乎与其他存储包一致(版本号)。 Only the naming is a bit off.只是命名有点偏离。 There's also a blog post from MS in June this year talking about this package being brand new and all, so I guess this is what I was looking for?今年 6 月,MS 还发表了一篇博文,谈到这款 package 是全新的,所以我想这就是我要找的东西?

Anyway: I found the API of Azure.Data.Tables quite similar to WindowsAzure.Storage , I just had to make a few tweaks here and there.无论如何:我发现 Azure.Data.Tables 的APIWindowsAzure.Storage非常相似,我只需要在这里和那里进行一些调整。

But what I am totally missing now is the classes QueryComparisons and TableQuery .但是我现在完全缺少的是QueryComparisonsTableQuery类。 Are they just gone?他们刚刚走了吗? Is there are replacement?有替代品吗?

An example of the code to migrate would be:要迁移的代码示例如下:

var part1 = 
  TableQuery.GenerateFilterConditionForGuid(
    nameof(SomeEntity.Id),
    QueryComparisons.Equal,
    idValue));
var part2 = 
  TableQuery.GenerateFilterConditionForGuid(
    nameof(SomeEntity.Category)
    QueryComparisons.Equal,
    category));
return TableQuery.CombineFilters(part1, TableOperators.And, part2);

I came across the same problem.我遇到了同样的问题。 Yes, TableQuery from Microsoft.Azure.Cosmos.Tables is gone, and the references in documentation to TableOdataFilter are useless as it doesn't exist.是的,来自Microsoft.Azure.Cosmos.TablesTableQuery已经消失,并且文档中对TableOdataFilter的引用是无用的,因为它不存在。

Looking at this, I found the easiest way to query Azure Storage Tables using the Azure.Data.Tables package is to use the .Query<T>([predicate]) method .看着这个,我发现使用Azure.Data.Tables package 方法查询 Azure 存储表的最简单方法是使用.Query<T>([predicate])方法 eg例如

TableClient customers;

var example = customers.Query<Customer>(c => c.Name == "Microsoft");

There is also a .QueryAsync<T>(..) async version.还有一个.QueryAsync<T>(..)异步版本。

If you were building queries with the TableQuery there is no direct alternative, but you can use something like PredicateBuilder to combine .And() and .Or() expressions to create the predicate.如果您使用TableQuery构建查询,则没有直接的替代方法,但您可以使用PredicateBuilder之类的东西来组合.And().Or()表达式来创建谓词。

I modified this to create a fluent version:我修改了这个以创建一个流畅的版本:

    /// <summary>
    /// Helper class to build predicates fluently
    /// </summary>
    /// <remarks>
    /// Adapted from source: http://www.albahari.com/nutshell/predicatebuilder.aspx
    /// </remarks>
    public class PredicateBuilder<T>
    {
        private Expression<Func<T, bool>> expression;

        /// <summary>
        /// Add an OR clause
        /// </summary>
        /// <param name="expr">new expression</param>
        /// <returns></returns>
        public PredicateBuilder<T> Or(Expression<Func<T, bool>> expr)
        {
            if (expression == null)
                expression = expr;
            else
            {
                var invokedExpr = Expression.Invoke(expr, expression.Parameters.Cast<Expression>());
                expression = Expression.Lambda<Func<T, bool>>(Expression.OrElse(expression.Body, invokedExpr), expression.Parameters);
            }
            return this;
        }

        /// <summary>
        /// Add an AND clause
        /// </summary>
        /// <param name="expr">new expression</param>
        /// <returns></returns>
        public PredicateBuilder<T> And(Expression<Func<T, bool>> expr)
        {
            if (expression is null)
                expression = expr;
            else
            {
                var invokedExpr = Expression.Invoke(expr, expression.Parameters.Cast<Expression>());
                expression = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expression.Body, invokedExpr), expression.Parameters);
            }
            return this;
        }

        /// <summary>
        /// Return resulting expression
        /// </summary>
        /// <returns></returns>
        public Expression<Func<T, bool>> ToPredicate()
        {
            if (expression is null)
                return (T value) => true;
            return expression;
        }
    }

Example usage:示例用法:

var query = new PredicateBuilder<Customer>();

if(name != null)
  query.And(c => c.Name == name);

if (isActive == false)
  query.And(c => c.IsActive == false);

Hope this helps希望这可以帮助

You should probably have read a little further in that blog post and the related docs :您可能应该在该博客文章相关文档中进一步阅读:

Querying Table Entities查询表实体

The TableClient allows the user to create custom queries using OData filters. TableClient 允许用户使用 OData 过滤器创建自定义查询。

string MyPK = "markers";
string MyRK = "id-001";
string filter = TableOdataFilter.Create($"PartitionKey eq {MyPK} or RowKey eq {MyRK}")

Pageable<TableEntity> entities = tableClient.Query<TableEntity>(filter: filter);

foreach (TableEntity entity in entities)
{
    Console.WriteLine($"{entity.GetString("Product")}: {entity.GetInteger("Count")}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 WindowsAzure.Storage 迁移到 Azure.Storage 包时从 CloudStorageAccount 引用服务 - Referencing services from CloudStorageAccount when migrating from WindowsAzure.Storage to Azure.Storage packages 我应该使用哪个版本的WindowsAzure.Storage? - Which version of WindowsAzure.Storage should I use? 共享访问从 windowsazure.storage 移动到 Azure.Storage.Files - Shared Access moving from windowsazure.storage to Azure.Storage.Files 在 C# .NET Core 中使用 Azure TableStorage - Nuget Package WindowsAzure.Storage Alternative - Using Azure TableStorage in C# .NET Core - Nuget Package WindowsAzure.Storage Alternative 如何在Visual Studio 2013中安装WindowsAzure.Storage? - How do I install WindowsAzure.Storage in Visual Studio 2013? WindowsAzure.Storage没有使用.Net Core 1.0 - WindowsAzure.Storage on not working on .Net Core 1.0 WindowsAzure.Storage使用C#mono - WindowsAzure.Storage using C# mono ExecuteQuery WindowsAzure.Storage 5.0.3没有扩展方法 - ExecuteQuery WindowsAzure.Storage 5.0.3 no extension method WindowsAzure.Storage,版本= 9.3.0.0-异常无法加载文件或程序集 - WindowsAzure.Storage, Version=9.3.0.0 - exception could not load file or assembly 将WindowsAzure.Storage安装到针对Xamarin平台的PCL - Install WindowsAzure.Storage to PCL targeting Xamarin platforms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM