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”.
In the New ASP.NET Project dialog box select the MVC template.
Click 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.
Now create a class file under the Model folder to create the customerInfo class.
Now create a class file named "CustomerInfo" and click the add button. Then create a Model class for it:
- public class CustomerInfo
- {
- [Key]
- public int CustomerID { get; set; }
- public string CustomerName { get; set; }
- public string CustAddress { get; set; }
- public string CustCity { get; set; }
- public int CustPinCode { get; set; }
- public string PhoneNo { get; set; }
- }
Now add another class file named "CustomerInfoContext" under the Model Folder and provide the following code for it:
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
-
- namespace EFRndProject.Models
- {
- public class CustomerInfoContext : DbContext
- {
-
- public CustomerInfoContext() : base("name=CustomerInfoContext")
- {
- }
-
- public System.Data.Entity.DbSet<EFRndProject.Models.CustomerInfo> CustomerInfo { get; set; }
-
- }
- }
We then need to change the web.config file to set the connection string:
- <connectionStrings>
- <add name="CustomerInfoContext" connectionString="Data Source=local;Initial Catalog=MVCPROJECT;word=a;Integrated Security=True" providerName="System.Data.SqlClient" />
- </connectionStrings>
Now click on the Controller folder and add an Emty Controller on it with the name "CustomerInfo" and now provide the following code:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using EFRndProject.Models;
-
- namespace EFRndProject.Controllers
- {
- public class CustomerInfoController : Controller
- {
- private CustomerInfoContext db = new CustomerInfoContext();
-
-
- public ActionResult Index()
- {
- return View(db.CustomerInfoes.ToList());
- }
- }
- }
Now add a View named Index within the Views -> CustomerInfo folder and provide the following code:
- @model IEnumerable<EFRndProject.Models.CustomerInfo>
-
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Index</h2>
-
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.CustomerName)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustAddress)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustCity)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustPinCode)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.PhoneNo)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustAddress)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustCity)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustPinCode)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.PhoneNo)
- </td>
- </tr>
- }
-
- </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: