Thoughts from the Wet Coast

The musings of an ASP.NET Developer from Canada's We(s)t Coast

Look Mom, NoSQL! - 5. My First RavenDB Application (1)

Category: ASP.NET Data
Last Modified: May 2 2017
Nov 18 2011

In my continuing series on NoSQL Databases I have been mainly describing some of the concepts behind NoSQL (and in particular Document) Databases.  In this post I will start to dive into some code.

As mentioned earlier I will be using RavenDB as this is a .NET friendly database.

In this first example lets create a simple ASP.NET MVC 3 Application that uses RavenDB as its data source.

I will start with a simple “My Tasks” Application which I created by using the Internet Application template in ASP.NET MVC 3.  Next I created a simple Task model class (Listing 1).

Listing 1: The Task class

public class Task {     public int TaskId { get; set; }     public string Title { get; set; }     public string Description { get; set; }     public bool IsComplete { get; set; } }

I then used the Scaffolding available in ASP.NET MVC 3 to create a TaskController (Figure 1), as well as the Entity Framework DataContext class and all the Razor Views necessary for CRUD operations.

Figure 1: Scaffold a new TaskController class


And finally I created a SQL Server Compact Edition Database to store the data and a Tasks table with columns that matched the names and types of the Task class’s properties.

To prove that everything was wired up correctly I browsed to the application and added some data (Figure 2).

Figure 2: A List of Tasks


But the goal of this application is to use RavenDB as the database, not to create an EF Code First/SQL CE application.

As I mentioned in the 3rd post in this series, RavenDB can be configured in three different ways.  I am going to use the embedded mode.  Luckily, RavenDB is available as a NuGet package.  To add a NuGet package to a Visual Studio project we can right-click on the References folder (Figure 3).

Figure 3: Add a new Nuget package


and select “Manage NuGet Packages”.  In the Package Manager window enter Raven  in the search box and install the RavenDB (Embedded) package (Figure 4).

Figure 4: Install RavenDB using NuGet


The installation adds quite a few assemblies to the bin folder.

Next we need to configure Raven, so we will create a Folder in the App_Data folder call “Database” (Figure 5),

Figure 5: Create a new Database Folder for the RavenDB database


and we will modify the connection string that RavenDB added to the web.config to point to the folder we just created.

Listing 2: Update connection string in web.config


So now we are all set up ready to convert our application to use RavenDB, and in the next post I will show how to hook up the existing TaskController class, so we can store our tasks in an embedded RavenDB database instead of a SQL Compact database.


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.