使用Azure新的NoSQL – DocumentDB(還在preview版本中)
怎們建立網頁上面有說. 這裡只記錄一些操作上的動作
- DocumentDB裡面有分幾個元素 Database, DocumentCollection, document
元素之間的關係
1 | Database -> DocumentCollection(with id name=xxxx) -> document(s) |
string dataBaseName = 「testDB」; var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
var database = client.CreateDatabaseQuery().FirstOrDefault(db => db.Id == dataBaseName);
if (database == null) { database = await client.CreateDatabaseAsync( new Database { Id = dataBaseName }); }
1 | ## 這個寫法如果用在webapi上面,會有出現等不到回應的情形發生,所以解決方法是變成同步, 寫法會變成 |
database = client.CreateDatabaseAsync(new Database{Id = dataBaseName}).Result;
1 |
|
User _user = new User(){name=『abc』}; var document1 = await client.CreateDocumentAsync(documentCollectionLink, _user);
這裡的documentCollectionLink是指 DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink).FirstOrDefault(db => db.Id == dataBaseName); 或者是剛新增出來的collection DocumentCollection collection = client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = 「some name」 });
documentCollectionLink = collection.SelfLink;
1 |
|
var doc = Client.CreateDocumentQuery
return Client.ReplaceDocumentAsync(doc.SelfLink, item);
1 |
|
var doc = Client.CreateDocumentQuery
return Client.DeleteDocumentAsync(doc.SelfLink);
1 |
|
Client.CreateDocumentQuery