Working With Model Class and Connection String in MVC 5

Introduction

This article introduces how to work with Models and Connection Strings. This article will help you to add some classes for the application that help manage databases and to create or maintain the Connection Strings that provide the path to connect to a database.

Let's start first by adding a class in the Model. The class will be the "model" part of the ASP.NET MVC application. The prerequisites for working in Model are the following:

  • MVC 5 template for creating project
  • Visual Studio 2013 Preview 
  • Entity Framework

Model Classes

In this section you will learn about adding a Model Class in a MVC 5 application. You should have an MVC 5 application for working in it.

Step 1: Go to Solution Explorer and select your app.

solution explorer.jpg

Step 2: Right-click on Model and select go "Add" then "Class".

add class.jpg

Step 3: Enter your class name.

class name.jpg

You can see your new class as in the following:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MvcCricket.Models  
  6. {  
  7.     public class Cricketer  
  8.     {  
  9.     }  
  10. } 

After adding the class in the Model, let's add some properties: 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MvcCricket.Models  
  6. {  
  7.     public class Cricketer  
  8.     {  
  9.         public int ID { getset; }  
  10.         public string Name { getset; }  
  11.         public int ODI { getset; }  
  12.         public int Test { getset; }  
  13.     }  
  14. } 

The Cricketer class represents Crickerters in the database. In the preceding example, each instance of the Cricketer object represents a row element within a database table and each property of this Cricketer class will map to a column in the table.

Now, after adding the Model class add the CricketerDBContext class in the same Cricketer class file:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MvcCricket.Models  
  6. {  
  7.     public class Cricketer  
  8.     {  
  9.         public int ID { getset; }  
  10.         public string Name { getset; }  
  11.         public int ODI { getset; }  
  12.         public int Test { getset; }  
  13.     }  
  14.     public class CricketerDBContext : DbContext  
  15.     {  
  16.         public DbSet<Cricketer> Cricketers { getset; }  
  17.     }  
  18. } 

The class named CricketerDBContext class corresponds to the Entity Framework cricketer database context. That is helpful for retrieving, storing and updating the Cricketer class instances in the database. The DbContext class that is provided by the Entity Framework is the base class of CricketerDBConetext.

For using the Entity Framework in this app or accessing the DbContext and DbSet, you need to add the following statement:

  1. using System.Data.Entity;  
Visual Studio 2013 Preview also provides the facility to manually add an assembly reference. For adding manually review the following statements:

Right-click on the red squiggly lines and select "Resolve" and then click the corresponding assembly reference.

assembly.jpg

or

Click on the red squiggly lines and move your cursor to the stating point and you'll notice that it'll show you to resolve the problem.

assembly1.jpg

Note: In the application there are many using statements that are unused. So, you can remove the unused using statements by right-clicking on your app and select "Organize Usings" and click "Remove Unused Usings":

using.jpg

You can add a model using the the preceding information.

Creating a Connection String

In the application, the CricketerDBConetext class is responsible for handling the task of connecting the database and to map the Cricketer objects to database records. The Entity Framework automatically connects the database and uses it. The default database is LocalDB. Now, I am adding a connection string explicitly in the Web.Config file in the application.

Working with SQL Server Express LocalDB

The SQL Server Express Database Engine has the LocalDB. It is very lightweight that automatically starts on demand and runs in the user mode. SQL Server Express has the special execution mode in which LocalDB runs that facilitates you to work with the database as .mdf files. The .mdf file of any database is stored in the App_data folder in the application.

You should not use the LocalDB in particular for production with a web application because it is designed to work with Internet Information Services (IIS). You can easily migrate your LocalDB database to SQL Server or SQL Azure. It is installed by default in Visual Studio 2012 and 2013 Preview versions of Visual Studio.

You can add a connection string to your app using the following procedure.

Step 1: Open your project

Step 2: In Solution Explorer open the Web.Config file

solution explorer.jpg

Step 3: Find the <connectionStrings> element in the file.

webconfig.jpg

Step 4: Add the following connection string to the <connectionStrings> element after the previous connection string:

  1. <add name="CricketerDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\Cricketers.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />   

Now there are two connection strings available in your Web.Config file. The connection string named DefaultConnection is used for the membership database and the connection string named CricketerDBConetext determines a LocalDB database that is located in the App_Data folder as Cricketers.mdf.

Note: You need to note that the connection string must match the name of DbContext class.

cricket.jpg

Summary

In this article you learned the way to add a Model Class in your MVC project template and to add a connection string in the web.Config file. I'll write further articles on MVC 5.


Similar Articles