public static bool UpdateOrder(Order order) { using (var context = new Db.OpenRentEntities()) { var dbOrder = context.Order.SingleOrDefault(x => x.Guid == order.Guid); if (dbOrder == null) { return(false); } dbOrder.Name = order.Name; dbOrder.RentBeginDate = order.RentBeginDate; dbOrder.RentEndDate = order.RentEndDate; dbOrder.TotalCost = order.TotalCost; dbOrder.AreaGuid = order.Area.Guid; dbOrder.PaymentTypeId = (int)order.PaymentType; var dbAddServices = new List <Db.OrderAdditionalServices>(); var dbRemoveServices = new List <Db.OrderAdditionalServices>(); foreach (var dbService in dbOrder.OrderAdditionalServices) { if (!order.AdditonalServiceGuids.Contains(dbService.ServiceGuid)) { dbRemoveServices.Add(dbService); } } foreach (var serviceGuid in order.AdditonalServiceGuids) { if (dbOrder.OrderAdditionalServices.Count(x => x.ServiceGuid == serviceGuid) == 0) { dbAddServices.Add(new Db.OrderAdditionalServices { Guid = Guid.NewGuid(), OrderGuid = order.Guid, ServiceGuid = serviceGuid }); } } foreach (var dbRemoveService in dbRemoveServices) { dbOrder.OrderAdditionalServices.Remove(dbRemoveService); context.Entry(dbRemoveService).State = System.Data.Entity.EntityState.Deleted; } foreach (var dbAddService in dbAddServices) { dbOrder.OrderAdditionalServices.Add(dbAddService); } return(true); } }
public static void DeleteCar(Guid carGuid) { using (var context = new Db.OpenRentEntities()) { var dbCar = context.Car.SingleOrDefault(x => x.Guid == carGuid); if (dbCar == null) { return; } context.Car.Remove(dbCar); context.Entry(dbCar).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } }