Click here to Skip to main content
15,936,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am developing simple task using different layers and everything is working fine but I have scenario where I need to insert to two tables when click on save ,one employee table and other employeeposition table. Here is my code.
In Service class
C#
public void Update(EmployeeViewModel employeeViewModel)
        {
            Employee employee = new Employee()
            {
                EmployeeId = employeeViewModel.EmployeeId,
                FirstName = employeeViewModel.FirstName,
                LastName = employeeViewModel.LastName,
                PhoneNumber = employeeViewModel.PhoneNumber,
                Address = employeeViewModel.Address,
                EmployeePositions = new List 
                {
                    new EmployeePosition 
                    {
                        PositionId=employeeViewModel.PositionId,
                        EmployeeId=employeeViewModel.EmployeeId
                    }
                }
            };           

            if (employee == null)
                throw new ArgumentNullException("Employee Update");
            _employeeRepository.Update(employee);
            _employeePositionRepository.UpdateCollection(employee.EmployeePositions);
        }

In Repository Class
C#
public void UpdateCollection(ICollection collection)
        {
            if (collection == null)
                throw new ArgumentNullException("collection is null");
            foreach (var item in collection)
            {
                DataContext.Entry(item).State = EntityState.Deleted;
            }
            this.DataContext.SaveChanges();
        }
and COntroller class

C#
[HttpGet]
        public ActionResult Edit(int? id)
        {
            var employee = _employeeService.GetById((int)id);
            if (employee == null) throw new ArgumentNullException("Employee Information Not Found");
            return View(employee);
        }

        [HttpPost]
        public ActionResult Edit(EmployeeViewModel employee)
        {
            _employeeService.Update(employee);
            return RedirectToAction("View", "Employee");
        }


Now when ever update happens I need to delete records in employeeposition table for that employeeid and insert it.
How can I do this
Posted
Updated 26-Jan-15 23:39pm
v4

1 solution

It seems you just trying to edit data for a particular EmployeeId. As you said "when ever update happens I need to delete records in employeeposition table for that employeeid and insert it." For that often we just edit or update that record. And I have done it using repository pattern in below articles:

WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 1[^]

CRUD Operations using Partial View and jQueryUI in ASP.NET MVC4 - Part 1[^]

If you are using ADO.NET or other DB you need to write your query accordingly in Repository. Thanks.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900