Friday, 27 December 2013

Creating a Model using entity framework in MVC

         In this article we are going to see how to create a model using entity framework and used it in the Application. Let we take a scenario that the List out the employee information based on the employee id as input.


Sql  Script:

CREATE TABLE EMPTABLE
(
      ID INT IDENTITY(1,1),
      NAME VARCHAR(200),
      COUNTRY VARCHAR(400),
      MARRIED VARCHAR(200)
)

INSERT INTO EMPTABLE(NAME,COUNTRY,MARRIED)
VALUES('RAJESH','INDIA','NO')
INSERT INTO EMPTABLE(NAME,COUNTRY,MARRIED)
VALUES('SURESH','US','NO')
INSERT INTO EMPTABLE(NAME,COUNTRY,MARRIED)
VALUES('KISHORE','US','YES')
INSERT INTO EMPTABLE(NAME,COUNTRY,MARRIED)
VALUES('PREM KUMAR','UK','NO')




Html:
@model TestingMvc.Models.Employee

@{
    ViewBag.Title = "Employee Data";
}

<h2>Employee Information</h2>

<table style="border:2px solid gray">
<tr>
<td><b>ID</b></td><td style="padding:5px">@Model.EmployeeId</td>
</tr>
<tr>
<td><b>Name</b></td><td style="padding:5px">@Model.EmployeeName</td>
</tr>
<tr>
<td><b>Country</b></td><td style="padding:5px">@Model.Country</td>
</tr>
<tr>
<td><b>Married</b></td><td style="padding:5px">@Model.Married</td>
</tr>
</table>



Now we concentrate on Code part:

Create a Model Class for Employee and the parameters and map that with actual name of the table and column with the attributes.

[Table("EMPTABLE")]
    public class Employee
    {
        [Column("ID")]
        public int EmployeeId { set; get; }

        [Column("NAME")]
        public string EmployeeName { set; get; }

        [Column("COUNTRY")]
        public string Country { set; get; }

        [Column("MARRIED")]
        public string Married { set; get; }
    }



Now create a context for that Employee Model class.

class should be derived from the Dbcontext then add the properites with DbSet Type, which means it take the set of collection values from the Model class Employee

    public class EmployeeDbContext:DbContext
    {
        public DbSet<Employee> Employees { set; get; }
    }



Now add the web.config connection string the name should be same as the context, so at the runtime engine will search for that name in the connection string. Place the connection string in the root web.config.

<connectionStrings>
    <add name="EmployeeDbContext" connectionString="Data Source=rajesh-PC\sqlserver2008;Initial Catalog=SampleDb;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
  </connectionStrings>

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestingMvc.Models;

namespace TestingMvc.Controllers
{
    public class EmployeeController : Controller
    {              
  public ActionResult Details()
        {
            List<Employee> emp = (new EmployeeDbContext()).Employees.ToList();
            return View(emp);
        }

    }
}  

Output:





From this article you can learn how to implement the entity framework for creating a Model and use it application.


No comments:

Post a Comment