title | description | services | author | ms.author | ms.service | ms.component | ms.custom | ms.devlang | ms.topic | clicktale | ms.date |
---|---|---|---|---|---|---|---|---|---|---|---|
Build a .NET web app with Azure Cosmos DB using the SQL API |
In this quickstart, use the Azure Cosmos DB SQL API and the Azure portal to create a .NET web app |
cosmos-db |
SnehaGunda |
sngun |
cosmos-db |
cosmosdb-sql |
quick start connect, mvc, devcenter |
dotnet |
quickstart |
true |
04/10/2018 |
[!div class="op_single_selector"]
Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.
This quick start demonstrates how to create an Azure Cosmos DB SQL API account, document database, and collection using the Azure portal. You'll then build and deploy a todo list web app built on the SQL .NET API, as shown in the following screenshot.
If you don’t already have Visual Studio 2017 installed, you can download and use the free Visual Studio 2017 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
[!INCLUDE quickstarts-free-trial-note] [!INCLUDE cosmos-db-emulator-docdb-api]
[!INCLUDE cosmos-db-create-dbaccount]
[!INCLUDE cosmos-db-create-collection]
[!INCLUDE cosmos-db-create-sql-api-add-sample-data]
[!INCLUDE cosmos-db-create-sql-api-query-data]
Now let's switch to working with code. Let's clone a SQL API app from GitHub, set the connection string, and run it. You'll see how easy it is to work with data programmatically.
-
Open a command prompt, create a new folder named git-samples, then close the command prompt.
md "C:\git-samples"
-
Open a git terminal window, such as git bash, and use the
cd
command to change to the new folder to install the sample app.cd "C:\git-samples"
-
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
git clone https://github.com/Azure-Samples/documentdb-dotnet-todo-app.git
-
Then open the todo solution file in Visual Studio.
This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to Update your connection string.
The following snippets are all taken from the DocumentDBRepository.cs file.
-
The DocumentClient is initialized on line 76.
client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]);
-
A new database is created on line 91.
await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
-
A new collection is created on line 110.
await client.CreateDocumentCollectionAsync( UriFactory.CreateDatabaseUri(DatabaseId), new DocumentCollection { Id = CollectionId }, new RequestOptions { OfferThroughput = 400 });
Now go back to the Azure portal to get your connection string information and copy it into the app.
-
In the Azure portal, in your Azure Cosmos DB account, in the left navigation select Keys, and then select Read-write Keys. You'll use the copy buttons on the right side of the screen to copy the URI and Primary Key into the web.config file in the next step.
-
In Visual Studio 2017, open the web.config file.
-
Copy your URI value from the portal (using the copy button) and make it the value of the endpoint key in web.config.
<add key="endpoint" value="FILLME" />
-
Then copy your PRIMARY KEY value from the portal and make it the value of the authKey in web.config.
<add key="authKey" value="FILLME" />
-
Then update the database value to match the name of the database you have created earlier. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.
<add key="database" value="Tasks" />
-
In Visual Studio, right-click on the project in Solution Explorer and then select Manage NuGet Packages.
-
In the NuGet Browse box, type DocumentDB.
-
From the results, install the Microsoft.Azure.DocumentDB library. This installs the Microsoft.Azure.DocumentDB package as well as all dependencies.
-
Select CTRL + F5 to run the application. Your app displays in your browser.
-
Select Create New in the browser and create a few new tasks in your to-do app.
You can now go back to Data Explorer and see query, modify, and work with this new data.
[!INCLUDE cosmosdb-tutorial-review-slas]
[!INCLUDE cosmosdb-delete-resource-group]
In this quickstart, you've learned how to create an Azure Cosmos DB account, create a collection using the Data Explorer, and run a web app. You can now import additional data to your Cosmos DB account.
[!div class="nextstepaction"] Import data into Azure Cosmos DB