articles

Home / DeveloperSection / Articles / How to access data in MVC using Entity framework

How to access data in MVC using Entity framework

marcel ethan 3171 06-Jun-2016

Step 1: First we create a table Employee:

create table Employee (Empid int primary key identity,Name nvarchar(50),Gender nvarchar(50),City nvarchar(50))

and Insert some data :

insert into Employee values('Ashish','Male','Alld')
insert into Employee values('Anand','Male','Alld')
insert into Employee values('Ashtosh','Male','Lko')
insert into Employee values('Harshit','Male','Lko')
insert into Employee values('Ashta','Female','Lko')
insert into Employee values('Niharika','Female','Lko')

How to access data in MVC using Entity framework

Step 2:  Open visual studio create new project and click Asp.Net MVC 4 Web application and give a name as MVCDemo :

How to access data in MVC using Entity framework

Inside the model folder create a class name as Employee.cs , inside the class create four properties

using System;        
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
 
namespace MvcDemo.Models
{
 
     publicclassEmployee
    {
       [Key]
 
        publicint EmpId { get; set; }
        publicstring Name { get;set;}
        publicstring Gender { get; set; }
        publicstring City { get; set; }
       
    }
}

Step3: Before using the Entity framework first we install Entity framework using NuGget package manager

Click the tool menu -> Click NuGet package manager -> Click Manage NuGet packages for solution

Note: In my machine entity framework already install that is why in my system screen tick mark but if is not install then you get Install button. Once you click the button automatic install the package in your solution and should have a reference in assembly.

How to access data in MVC using Entity framework

Step4: And after that right click the model folder create a class file name as EmployeeContext.cs                          

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
 
 
namespace MvcDemo.Models
{
   
    publicclassEmployeeContext :DbContext
    {
      
        publicDbSet<Employee> Employee
        {
            get;
            set;
        }
    }
}

 

Note: Purpose of the EmployeeContext.cs class established a connection of the database.

Step5: After that add a connectionstrings in the web.config file

<connectionStrings>
    <addname="EmployeeContext"connectionString="Data Source=.;User ID=sa; password=****;Initial Catalog=NewForm"providerName="System.Data.SqlClient" /> 
   
  </connectionStrings>

And then mapped the employee model class to database table Employee using table attribute [Table("Employee ")] and import the namespace using System.ComponentModel.DataAnnotations.Schema; 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
 
namespace MvcDemo.Models
{
 
    [Table("employee")]
 
    publicclassEmployee
    {
       [Key]
 
        publicint EmpId { get; set; }
        publicstring Name { get;set;}
        publicstring Gender { get; set; }
        publicstring City { get; set; }
       
    }
}

Step6 : And then right click on the Controllers and create a EmployeController.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDemo.Models;
using System.ComponentModel.DataAnnotations.Schema;
 
namespace MvcDemo.Controllers
{
    publicclassEmployeeController : Controller
    {
        publicActionResult Details(int id)
        {
            EmployeeContext employeeContext = newEmployeeContext();
            Employee employee = employeeContext.Employee.Single(emp => emp.EmpId == id);
           
            return View(employee);
 
           
        }
 
    }
}

Step7 : Create a View : Right click on the Details MethodĂ click the AddView

@model MvcDemo.Models.Employee
 
@{
    ViewBag.Title = "Employee Details";
}
 
<h2><bstyle="color: red">Employee Details </b></h2>
<tablestyle="color: orange; border: double">
    <tr>
        <td>
            <bstyle="color: green">Employee Id : </b>
        </td>
        <td>
            <bstyle="color:black">@Model.EmpId </b>
        </td>
    </tr>
    <tr>
        <td>
            <bstyle="color: green">Employee Name : </b>
        </td>
        <td>
            <bstyle="color: black">@Model.Name </b>
        </td>
    </tr>
    <tr>
        <td>
            <bstyle="color: green">Gender </b>
        </td>
        <td>
            <bstyle="color: black">@Model.Gender </b>
        </td>
    </tr>
    <tr>
        <td>
            <bstyle="color: green">City </b>
        </td>
        <td>
            <bstyle="color: black">@Model.City </b>
        </td>
    </tr>
</table>


And run your application

http://localhost:51313/employee/Details/1

 

Output:

How to access data in MVC using Entity framework

If you change the id of url output will be also change   :

http://localhost:51313/employee/Details/2<-----

How to access data in MVC using Entity framework


Updated 07-Sep-2019

Leave Comment

Comments

Liked By