2013-08-22 40 views
5

Cách tốt nhất để sao chép tất cả các hàng từ một bảng sang bảng khác là gì?
tôi đã cố gắng mã dưới đây để có được tất cả các hàng trong một bảng:Sao chép tất cả các hàng sang bảng khác trong bộ lưu trữ bảng Azure

TableServiceContext _dataContext; 
    public IEnumerable<T> GetAllEntities() 
    { 
     IQueryable<T> query = null; 
     try 
     { 
      query = _dataContext.CreateQuery<T>(_tableName); 
     } 
     catch (Exception ex) 
     { 

     } 
     return query.ToArray(); 
    } 

nhưng nó không nhận được hàng hơn khoảng 900. Tôi có vài trăm ngàn dòng.
Mã Cập nhật:

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

}

nhưng với lỗi:

Error 121 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TElement' in the generic type or method 'Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteQuerySegmented

Trả lời

7

Lý do bạn đang nhận được chỉ 900 kết quả lại là bởi vì bạn đang chạy vào thẻ tiếp tục. Theo mặc định, một yêu cầu duy nhất cho dịch vụ bảng sẽ trả về tối đa 1000 thực thể. Nó có thể là ít hơn 1000 thực thể (và thậm chí 0) nhưng không bao giờ nhiều hơn 1000. Nếu có nhiều thực thể có sẵn, thì dịch vụ bảng trả về một continuation token cần được sử dụng để tìm tập hợp các thực thể tiếp theo.

Vì vậy, mã của bạn nên tìm mã thông báo tiếp tục và nên tiếp tục tìm nạp các thực thể cho đến khi mã thông báo thời gian được trả về bởi dịch vụ bảng. Đừng hãy nhìn vào đoạn code mẫu dưới đây:

private IEnumerable<T> FetchAllEntities() 
{ 
    List<T> allEntities = new List<T>(); 
    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount; 
    CloudTable table = storageAccount.CreateCloudTableClient().GetTableReference("MyTable"); 
    Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = null; 
    do 
    { 
     var queryResponse = table.ExecuteQuerySegmented<T>(null, tableContinuationToken, null, null); 
     tableContinuationToken = queryResponse.ContinuationToken; 
     allEntities.AddRange(queryResponse.Results); 
    } 
    while (tableContinuationToken != null); 
    return allEntities; 
} 

Cập nhật

Đối với lỗi của bạn, hãy thử thay đổi sau

public class TableRepository<T> : IRepository<T> 
    where T : TableEntity 

để

public class TableRepository<T> : IRepository<T> 
    where T : TableEntity, new() 

thông báo việc bổ sung trong số new() sau TableEntity.

+0

Tôi nhận được lỗi này- Lỗi 'T' phải là loại không trừu tượng với hàm tạo tham số công khai để sử dụng tham số 'TElement' theo loại hoặc phương thức chung chung – Seenu

+0

Bạn có thể chia sẻ mã không ? –

+0

đã cập nhật câu hỏi với mã mới nhất – Seenu

Các vấn đề liên quan