2010-11-28 4 views
2

IDictionary가있는 클래스가 있습니다. 이 개체의 크기가 특정 크기로 수정되지 않았습니다. 이 아이디어는 내 개체의 동적 스키마 있습니다. 이 개체를 TableStorage에 저장하려고합니다. 어떻게해야합니까? 그리고 어떻게 IDictionary 다시 내 개체 내 에서이 정보를 저장소에서 검색 할 수 있습니까 ??Azure : IDictionary <String>을 TableStorage에 저장하는 방법?

감사합니다.

답변

0

기본적으로 사전은 직렬화 할 수 없습니다. TableStorage에 저장하려면 객체를 직렬화 할 수 있어야합니다. List 또는 Array 유형의 객체를 사용하거나 List 또는 Arrays가 충분하지 않은 경우 Dictionary에 대한 고유 한 직렬 변환기를 작성하는 것이 좋습니다.

0

TableStorageContext의 읽기 및 쓰기 이벤트를 사용하여이를 수행 할 수 있습니다. IDictionary 콘텐츠를 다른 필드 또는 BLOB 파일로 저장해야합니다. Read 이벤트에서는 주어진 필드에서 IDictionary를 만들거나 BLOB 파일에서 직접 검색합니다. Write 이벤트에서 IDictionary를 필드 또는 BLOB 파일로 저장합니다.

중요 : 엔터티 traslation 단계 후에 Write 이벤트가 발생하면 필드 방식을 선택하면 엔터티의 XML serialization에 직접 변경 내용을 기록해야 할 수 있습니다. 많은 시나리오에서

이 하드 들리지만 매우 유용는

0

DynamicTableEntity는 PARAM로 IDictionary<string,EntityProperty> 걸립니다.

이 코드는 LinqPad에서 실행 :

void Main() 
{ 
var account = ""; 
var key = ""; 
var tableName = ""; 

var storageAccount = GetStorageAccount(account, key); 
var cloudTableClient = storageAccount.CreateCloudTableClient(); 
var table = cloudTableClient.GetTableReference(tableName); 

var partitionKey = "pk"; 
var rowKey = "rk"; 

//create the entity 
var entity = new DynamicTableEntity(partitionKey, rowKey, "*", 
    new Dictionary<string,EntityProperty>{ 
      {"Prop1", new EntityProperty("stringVal")}, 
      {"Prop2", new EntityProperty(DateTimeOffset.UtcNow)}, 
     }); 

//save the entity 
table.Execute(TableOperation.InsertOrReplace(entity)); 

//retrieve the entity 
table.Execute(TableOperation.Retrieve(partitionKey,rowKey)).Result.Dump(); 
} 

static CloudStorageAccount GetStorageAccount(string accountName, string key, bool useHttps = true) 
{ 
var storageCredentials = new StorageCredentials(accountName, key); 
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps: useHttps); 
return storageAccount; 
} 
관련 문제