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

How to access data in MVC using Entity framework


ASP.Net ASP.Net  ASP.NET MVC 
Ratings:
0 Comment(s)
 614  View(s)
Rate this:

How to access data in MVC using Entity framework


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')


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


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

{

 

     public class Employee

    {

       [Key]

 

        public int EmpId { get; set; }

        public string Name { get;set;}

        public string Gender { get; set; }

        public string 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.


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

{

   

    public class EmployeeContext :DbContext

    {

      

        public DbSet<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>

    <add name="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")]

 

    public class Employee

    {

       [Key]

 

        public int EmpId { get; set; }

        public string Name { get;set;}

        public string Gender { get; set; }

        public string 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

{

    public class EmployeeController : Controller

    {

        public ActionResult Details(int id)

        {

            EmployeeContext employeeContext = new EmployeeContext();

            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><b style="color: red">Employee Details </b></h2>

<table style="color: orange; border: double">

    <tr>

        <td>

            <b style="color: green">Employee Id : </b>

        </td>

        <td>

            <b style="color:black">@Model.EmpId </b>

        </td>

    </tr>

    <tr>

        <td>

            <b style="color: green">Employee Name : </b>

        </td>

        <td>

            <b style="color: black">@Model.Name </b>

        </td>

    </tr>

    <tr>

        <td>

            <b style="color: green">Gender </b>

        </td>

        <td>

            <b style="color: black">@Model.Gender </b>

        </td>

    </tr>

    <tr>

        <td>

            <b style="color: green">City </b>

        </td>

        <td>

            <b style="color: black">@Model.City </b>

        </td>

    </tr>

</table>

And run your application

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

 

Output:


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

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



Don't want to miss updates? Please click the below button!

Follow MindStick