Build MVC Applications with Connectivity to Cosmos DB Data



This article shows how to use only the Entity Framework and the CData ADO.NET provider to access Cosmos DB from an ASP.NET MVC application.

In this article, we will guide you through the process of utilizing wizards within Visual Studio to seamlessly integrate the CData ADO.NET Provider for Cosmos DB into a basic MVC (Model, View, Controller) project.

Create the Entity Framework Model

Follow the steps below to save connection properties and map tables to entities in the data model.

  1. Create a new MVC project in Visual Studio. In this example, the project name is MvcCosmosDBApp.
  2. If you are using Entity Framework 6, you will need to take the preliminary step of registering the Cosmos DB Entity Framework provider for your project. See the "LINQ and Entity Framework" chapter in the help documentation for a guide.

    Note that MVC 3 scaffolding and MVC 4 scaffolding do not support Entity Framework 6. You can use your scaffolding with Entity Framework 6 by upgrading to the latest version of MVC.

  3. To add the .edmx file from the designer, right-click your Models folder and click Add New Item. Select ADO.NET Entity Data Model, name the model, and click Add. In this example, the name of the model is CosmosDBModel.
  4. In the Entity Data Model wizard, select the option 'EF Designer from database'. The Entity Data Model wizard is displayed.
  5. Click New Connection. Select CData Cosmos DB Data Source in the dialog that is displayed.
  6. Specify the required connection string properties.

    To obtain the connection string needed to connect to a Cosmos DB account using the SQL API, log in to the Azure Portal, select Azure Cosmos DB, and select your account. In the Settings section, click Connection String and set the following values:

    • AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account
    • AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. From the resource menu, go to the Keys page. Find the PRIMARY KEY value and set AccountKey to this value.

    A typical connection string is below:

    AccountEndpoint=myAccountEndpoint;AccountKey=myAccountKey;
  7. Name the connection and select whether to include sensitive information, such as connection credentials, in the connection string. For simplicity, this example saves sensitive information in Web.config. The connection settings are saved as CosmosDBEntities.

  8. Select the tables and views you need. In this example, Customers is imported. Also, the option to pluralize object names is deselected in this example. Click Finish to create the .edmx file.
  9. Build your project to complete this step.

Scaffold the Controller and Views

Once you've established the model and completed the project build, you can employ ASP.NET Scaffolding wizards to generate both the controller and the views.

  1. In Solution Explorer, right-click the controllers folder and click Add -> Controller. Select MVC 5 Controller with views, using Entity Framework.
  2. In the Add Controller dialog that is then displayed, select the following options:

    • Model class: Select a table you imported; for example, Customers.
    • Data context class: Select your context class.
  3. Leave the default values for the other fields.

You can now access the list of Customers records at http://MySite/Customers. Next to each record are links to edit, delete, and see more information. You can also create new Customers records. With every state change the site picks up any data changes.

Ready to get started?

Download a free trial of the Cosmos DB Data Provider to get started:

 Download Now

Learn more:

Cosmos DB Icon Cosmos DB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Cosmos DB document databases.