Working With Entity Framework 6.0 in MVC: Part 1

The Entity Relationship Model

The main benefit of Entity Framework is that it frees us from being concerned with the structure of the database. All of our data access and storage is done against a conceptual data model that reflects our own business objects. With ADO.Net DataReaders and many other data access technologies, we always spend a large development time to fetch data from a database as required and push the data into business classes. Using Entity Framework, we no longer query against the schema of a database. As data is retrieved, you are not forced to select columns and rows and push them into objects, because they are returned as objects. When it's time to save changes back to the database, you need to save only those objects. The Entity Framework does the necessary work of translating your objects back into the rows and columns of the relational store. The Entity Framework does this part of the job for you, similar to the way an Object Relational Mapping (ORM) tool works.

Using these articles, we first show how to create a simple web application that gets some data from the database and displays it in a tabular structure.

For these, we first create a MVC Web Application.

Open Visual Studio and create a new C# project named “EFRndProject”.

new project web

In the New ASP.NET Project dialog box select the MVC template.

Click Change Authentication.

change authentication

Back in the New ASP.NET Project dialog box, click OK to create the project.

Install Entity Framework 6

From the Tools menu click Nuget Package Manager and then click Manage Nuget Packages for Solution. Search for Entity Framework and click on the Install Button.

nuget package install

Now create a class file under the Model folder to create the customerInfo class.

add new model class

Now create a class file named "CustomerInfo" and click the add button. Then create a Model class for it:

  1. public class CustomerInfo   
  2.  {   
  3.       [Key]   
  4.       public int CustomerID { getset; }   
  5.       public string CustomerName { getset; }   
  6.       public string CustAddress { getset; }   
  7.       public string CustCity { getset; }   
  8.       public int CustPinCode { getset; }   
  9.       public string PhoneNo { getset; }   
  10.  }   
Now add another class file named "CustomerInfoContext" under the Model Folder and provide the following code for it: 
  1.  using System;   
  2.  using System.Collections.Generic;   
  3.  using System.Data.Entity;   
  4.  using System.Linq;   
  5.  using System.Web;   
  6.    
  7.  namespace EFRndProject.Models   
  8.  {   
  9.      public class CustomerInfoContext : DbContext   
  10.      {   
  11.    
  12.          public CustomerInfoContext() : base("name=CustomerInfoContext")   
  13.          {   
  14.          }   
  15.    
  16.          public System.Data.Entity.DbSet<EFRndProject.Models.CustomerInfo> CustomerInfo { getset; }   
  17.    
  18.      }   
  19.  }    
We then need to change the web.config file to set the connection string:

  1. <connectionStrings>   
  2.    <add name="CustomerInfoContext" connectionString="Data Source=local;Initial Catalog=MVCPROJECT;word=a;Integrated Security=True" providerName="System.Data.SqlClient" />   
  3.    </connectionStrings>  

Now click on the Controller folder and add an Emty Controller on it with the name "CustomerInfo" and now provide the following code:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Data;   
  4. using System.Data.Entity;   
  5. using System.Linq;   
  6. using System.Net;   
  7. using System.Web;   
  8. using System.Web.Mvc;   
  9. using EFRndProject.Models;   
  10.   
  11. namespace EFRndProject.Controllers   
  12. {   
  13.    public class CustomerInfoController : Controller   
  14.    {   
  15.       private CustomerInfoContext db = new CustomerInfoContext();   
  16.   
  17.       // GET: /CustomerInfo/   
  18.          public ActionResult Index()   
  19.          {   
  20.             return View(db.CustomerInfoes.ToList());   
  21.          }   
  22.    }   
  23. }  
Now add a View named Index within the Views -> CustomerInfo folder and provide the following code:
  1. @model IEnumerable<EFRndProject.Models.CustomerInfo>   
  2.   
  3. @{   
  4.    ViewBag.Title = "Index";   
  5.    Layout = "~/Views/Shared/_Layout.cshtml";   
  6. }   
  7.   
  8. <h2>Index</h2>   
  9.   
  10. <p>   
  11.    @Html.ActionLink("Create New", "Create")   
  12. </p>   
  13. <table class="table">   
  14.    <tr>   
  15.       <th>   
  16.          @Html.DisplayNameFor(model => model.CustomerName)   
  17.       </th>   
  18.       <th>   
  19.          @Html.DisplayNameFor(model => model.CustAddress)   
  20.       </th>   
  21.       <th>   
  22.          @Html.DisplayNameFor(model => model.CustCity)   
  23.       </th>   
  24.       <th>   
  25.          @Html.DisplayNameFor(model => model.CustPinCode)   
  26.       </th>   
  27.       <th>   
  28.          @Html.DisplayNameFor(model => model.PhoneNo)   
  29.       </th>   
  30.       <th></th>   
  31.    </tr>   
  32.   
  33. @foreach (var item in Model) {   
  34. <tr>   
  35.    <td>   
  36.        @Html.DisplayFor(modelItem => item.CustomerName)   
  37.    </td>   
  38.    <td>   
  39.          @Html.DisplayFor(modelItem => item.CustAddress)   
  40.    </td>   
  41.    <td>   
  42.          @Html.DisplayFor(modelItem => item.CustCity)   
  43.    </td>   
  44.    <td>   
  45.          @Html.DisplayFor(modelItem => item.CustPinCode)   
  46.    </td>   
  47.    <td>   
  48.          @Html.DisplayFor(modelItem => item.PhoneNo)   
  49.    </td>   
  50. </tr>   
  51. }   
  52.   
  53. </table>   
We will then insert some data into the customerInfo table from SQL Server using a script and then run the application. The final output is as below:

output index

 

 

Up Next
    Ebook Download
    View all
    Learn
    View all