ViewBag.Failed = "Delete Employee Failed" Public ActionResult DeleteEmployee(Employee model) Public ActionResult DeleteEmployee(int EmployeeId) Int result = _employeeRepository.UpdateEmployee(model) Public ActionResult EditEmployee(Employee model) Public ActionResult EditEmployee(int EmployeeId)Įmployee model = _employeeRepository.GetEmployeeById(EmployeeId) Return RedirectToAction("AddEmployee", "Employee") Return RedirectToAction("Index", "Employee") Int result = _employeeRepository.AddEmployee(model) Public ActionResult AddEmployee(Employee model) Var model = _employeeRepository.GetAllEmployee() _employeeRepository = employeeRepository Public EmployeeController(IEmployeeRepository employeeRepository) _employeeRepository = new EmployeeRepository(new Models.EmployeeContext()) Private IEmployeeRepository _employeeRepository Public class EmployeeController : Controller Protected virtual void Dispose(bool disposing)Īdd a EmployeeController which directly interact with Repository. Public void DeleteEmployee(int employeeId)Įmployee employeeEntity = _(employeeId) _context.Entry(employeeEntity).State = EntityState.Modified Public int UpdateEmployee(Employee employeeEntity) Public int AddEmployee(Employee employeeEntity) Public Employee GetEmployeeById(int studentId) Public EmployeeRepository(EmployeeContext context) Private readonly EmployeeContext _context Public class EmployeeRepository:IEmployeeRepository Int UpdateEmployee(Employee employeeEntity) ĮmployeeRepository.cs using RepositoryWithMVC.Models Int AddEmployee(Employee employeeEntity) Public interface IEmployeeRepository:IDisposable Add an interface and a class respectively IEmployeeRepository.cs and EmployeeRepository.cs IEmployeeRepository.cs using RepositoryWithMVC.Models Using Ĭreate a folder with name “ Repository” inside your project. You can create database and table manually or if you use my approach it will create database and table automatically when you run the application first time because of I have used here Code First approach. Provide the Name and Location for the project and Click NextĬhoose Basic template as project template and Click Ok Step 2Ĭreate a EmployeeContext.cs and Employee.cs entity class inside Model folder for database factory. Step 1Ĭreate a new project -> Web -> Visual Studio 2012 Today, I am going to explain how to use repository pattern in Asp.Net MVC with EntityFramework. The main advantage to use repository pattern to isolate the data access logic and business logic, so that if you make changes in any of this logic that cannot effect directly on other logic. Repository directly communicates with data access layer and gets the data and provides it to business logic layer. Repository Pattern is used to create an abstraction layer between data access layer and business logic layer of an application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |