示例#1
0
        public bool Update(Car carToUpdate)
        {
            using (DB_Car_RentalEntities ctx = new DB_Car_RentalEntities())
            {
                // option 1: find the employee, update the properties, save cahnges
                //var e = ctx.Employees.Find(employeeToUpdate.EmployeeID);
                //e.FirstName = employeeToUpdate.FirstName;
                //e = employeeToUpdate;

                // option 2: -- need to make the ctx as data member
                //var e = GetByID(employeeToUpdate.EmployeeID);
                //e.FirstName = employeeToUpdate.FirstName;
                //e = employeeToUpdate;

                // option 3: add the employee to the context

                // 1) attach the employee to the context (in the memory)
                ctx.Cars.Attach(carToUpdate);

                // 2) mark the employee as "need to be updated"
                ctx.Entry(carToUpdate).State = System.Data.EntityState.Modified;

                // 3) save the changes in the database
                int count = ctx.SaveChanges();

                return(count > 0);
            }
        }
        public bool Update(User userToUpdate)
        {
            using (DB_Car_RentalEntities ctx = new DB_Car_RentalEntities())
            {
                // 1) attach the employee to the context (in the memory)
                ctx.Users.Attach(userToUpdate);

                // 2) mark the employee as "need to be updated"
                ctx.Entry(userToUpdate).State = System.Data.EntityState.Modified;

                // 3) save the changes in the database
                int count = ctx.SaveChanges();

                return(count > 0);
            }
        }
示例#3
0
        public bool Delete(Order orderToDelete)
        {
            using (DB_Car_RentalEntities ctx = new DB_Car_RentalEntities())
            {
                // 1) attach the employee to the context (in the memory)
                ctx.Orders.Attach(orderToDelete);

                // 2) mark the employee as "need to be updated"
                ctx.Entry(orderToDelete).State = System.Data.EntityState.Modified;
                if (orderToDelete.IsActiv == true)
                {
                    orderToDelete.IsActiv = false;
                }

                // 3) save the changes in the database
                int count = ctx.SaveChanges();

                return(count > 0);
            }
        }