Created
November 22, 2015 11:15
-
-
Save Tesla9527/53649ac67e5d99a3edd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------- | |
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