Step 1: Run the PowerShell Scripts
Step 2: Use the new Azure portal at portal.azure.com
Step 3: Run NuGet: Install-Package Microsoft.Azure.Documents.Client -Pre
Step 4: This is the Flashcard class
public class FlashCard { [JsonProperty(PropertyName = "id")] public string ID { get; set; } [JsonProperty(PropertyName = "question")] public string Question { get; set; } [JsonProperty(PropertyName = "answer")] public string Answer { get; set; } }Step 5: This is the console application:
internal class Program { private static void Main(string[] args) { CreateSaveAndGet().Wait(); //CleanUp().Wait(); } public static async Task CreateSaveAndGet() { //connect database var client = GetClient(); //find or create a database var database = await GetDatabase(client); //create a collection var collection = await GetCollection(client, database); Console.WriteLine("Save new flashcards..."); var flashcard1 = await client.CreateDocumentAsync(collection.SelfLink, new FlashCard() { ID = "1", Question = "When did Azure DocumentDB release in preview?", Answer = "August 21st, 2014", }); var flashcard2 = await client.CreateDocumentAsync(collection.SelfLink, new FlashCard() { ID = "2", Question = "What is Azure DocumentDBs twitter handle?", Answer = "@DocumentDB", }); var flashcards = await Task.Run(() => client.CreateDocumentQuery(collection.DocumentsLink) .AsEnumerable() .ToList()); Console.WriteLine("Iterating through flashcards..."); foreach (var flashcard in flashcards) { Console.WriteLine("QUESTION " + flashcard.ID.ToString() + ": " + flashcard.Question); Console.WriteLine("Answer: " + flashcard.Answer); Console.WriteLine("Press a key"); Console.ReadKey(); } Console.ReadKey(); var flashcardGet = await Task .Run(() => client.CreateDocumentQuery (collection.DocumentsLink) .Where(d => d.ID == "1") .AsEnumerable() .FirstOrDefault()); Console.WriteLine(flashcardGet.Question); Console.ReadKey(); var doc = client.CreateDocumentQuery (collection.DocumentsLink) .Where(d => d.Id == flashcardGet.ID) .AsEnumerable().FirstOrDefault(); await client.DeleteDocumentAsync(doc.SelfLink); flashcardGet = await Task .Run(() => client.CreateDocumentQuery (collection.DocumentsLink) .Where(d => d.ID == "1") .AsEnumerable() .FirstOrDefault()); Console.WriteLine(flashcardGet.Question); Console.ReadKey(); } private static async Task CleanUp() { //connect database var client = GetClient(); //find or create a database var database = await GetDatabase(client); //create a collection var collection = await GetCollection(client, database); await client.DeleteDocumentCollectionAsync(collection.SelfLink); } private static DocumentClient GetClient() { string endpoint = ConfigurationManager.AppSettings["EndPoint"]; string authKey = ConfigurationManager.AppSettings["AuthKey"]; Uri endpointUri = new Uri(endpoint); var client = new DocumentClient(endpointUri, authKey); return client; } private static async Task GetDatabase(DocumentClient client) { Database database; var databaseName = "flashcards"; var databases = client.CreateDatabaseQuery() .Where(db => db.Id == databaseName).ToArray(); if (databases.Any()) { database = databases.First(); } else { database = new Database { Id = databaseName }; database = await client.CreateDatabaseAsync(database); } return database; } private static async Task GetCollection(DocumentClient client, Database database) { var collectionName = "flashcards"; DocumentCollection collection; var collections = client.CreateDocumentCollectionQuery(database.SelfLink) .Where(col => col.Id == collectionName).ToArray(); if (collections.Any()) { collection = collections.First(); } else { collection = await client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = collectionName }); } return collection; } }
great
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteVery good information. Its very useful for me. we need learn from real time examples and for this we choose good training institute, who were interested to know about Azure which is quite interesting. We need a good training institute for my learning .. so people making use of the free demo classes.
ReplyDeleteMany training institute provides free demo classes. One of the best training institute in Bangalore is Apponix Technologies.
https://www.apponix.com/Microsoft-Azure/Microsoft-Azure-Training-in-Bangalore.html