public static void Delete(int id) { using (var unitOfWork = new UnitOfWork()) { using (var items = new XPCollection <DB.Cars>(unitOfWork)) { var dbItem = items.FirstOrDefault(u => u.Id == id); if (dbItem == null) { throw new Exception(Resources.Cars_Read_Автомобиль_не_найден_); } items.DeleteObjectOnRemove = true; Damages.DeleteByCarId(unitOfWork, id); Rents.DeleteByCarId(unitOfWork, id); Orders.DeleteByCar(id); items.Remove(dbItem); } unitOfWork.CommitChanges(); } }
private static int Change(Car item) { using (var unitOfWork = new UnitOfWork()) { using (var items = new XPCollection <DB.Cars>(unitOfWork)) { var dbItem = items.FirstOrDefault(u => u.Id == item.Id); if (dbItem == null) { throw new Exception(Resources.Cars_Read_Автомобиль_не_найден_); } dbItem.Number = item.Number; dbItem.Sts = item.Sts; dbItem.Insurance = item.Insurance; dbItem.Kpp = item.Kpp; dbItem.Color = (int)item.Color; dbItem.Engine = item.Enginge; dbItem.TO = item.TO; dbItem.Year = item.Year; dbItem.Body = (int)item.BodyType; dbItem.BodyNumber = item.BodyNumber; dbItem.Mileage = item.Mileage; dbItem.SiteId = item.SiteId; dbItem.Deleted = item.Deleted; FillDependencies(unitOfWork, dbItem, item); } Damages.DeleteByCarId(unitOfWork, item.Id); Rents.DeleteByCarId(unitOfWork, item.Id); AddDependencies(unitOfWork, item, item.Id); if (item.Deleted) { Orders.SetDeletedByCar(item.Id); } unitOfWork.CommitChanges(); return(item.Id); } }