Skip to content

Instantly share code, notes, and snippets.

@Tesla9527
Created November 22, 2015 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tesla9527/53649ac67e5d99a3edd7 to your computer and use it in GitHub Desktop.
Save Tesla9527/53649ac67e5d99a3edd7 to your computer and use it in GitHub Desktop.
-----------------------------
Below is code in EmployeeContext.cs
-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WebApplication2.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
-----------------------------
Below is connectionString in web.config
-----------------------------
<connectionStrings>
<add name="EmployeeContext" connectionString="Data Source=localhost;database=Sample;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>
-----------------------------
Below is code in Employee.cs
-----------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace WebApplication2.Models
{
[Table("tblEmployee")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
-----------------------------
Below is code in EmployeeController.cs
-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
return View(employee);
}
}
}
-----------------------------
Below is code in Global.asax
-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Data.Entity;
namespace WebApplication2
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<WebApplication2.Models.EmployeeContext>(null);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment