将所有行从一个表复制到另一个表的最佳方法是什么?
我尝试了下面的代码来获取表中的所有行:
TableServiceContext _dataContext;
public IEnumerable<T> GetAllEntities()
{
IQueryable<T> query = null;
try
{
query = _dataContext.CreateQuery<T>(_tableName);
}
catch (Exception ex)
{
}
return query.ToArray();
}
但它的行数不会超过 900 行。我有几十万行。
更新代码:
public class TableRepository<T> : IRepository<T>
where T : TableEntity
{
protected readonly string _tableName;
protected readonly TableServiceContext _dataContext;
protected readonly CloudTable _tableReference;
public TableRepository(string tableName, CloudTableClient tableClient)
{
_tableName = tableName;
_dataContext = tableClient.GetTableServiceContext();
_tableReference = tableClient.GetTableReference(tableName);
_dataContext.ResolveType = ResolveEntityType;
_dataContext.MergeOption = System.Data.Services.Client.MergeOption.NoTracking;
}
public IEnumerable<T> GetAllEntities()
{
List<T> allEntities = new List<T>();
try
{
Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = null;
do
{
var queryResponse = _tableReference.ExecuteQuerySegmented<T>(null, tableContinuationToken, null, null);
tableContinuationToken = queryResponse.ContinuationToken;
allEntities.AddRange(queryResponse.Results);
}
while (tableContinuationToken != null);
}
catch (Exception ex)
{
throw new DALException(_tableName,_dataContext.BaseUri.OriginalString, "An error occured while querying data", ex);
}
return allEntities;
}
}
但有错误:
错误 121“T”必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法“Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteQuerySegmented”中的参数“TElement”