public static bool AddCar(Car car, IEnumerable <HttpPostedFileBase> fileUpload) { try { using (var ctx = new carShopEntities()) { ctx.Cars.Add(car); ctx.SaveChanges(); foreach (var item in fileUpload) { Image image = new Image() { ProductImage = item.FileName, CarId = car.CarId }; var fileName = Path.GetFileName(item.FileName); var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/images"), fileName); item.SaveAs(filePath); ctx.Images.Add(image); ctx.SaveChanges(); } } return(true); } catch (Exception) { return(false); } }
public static List <Image> RemoveCar(int id) { try { using (var ctx = new carShopEntities()) { Car query = (from que in ctx.Cars where que.CarId == id select que).FirstOrDefault(); ctx.Cars.Remove(query); List <Image> images = (from que in ctx.Images where que.CarId == id select que).ToList(); foreach (Image item in images) { ctx.Images.Remove(item); } ctx.SaveChanges(); return(images); } } catch (Exception) { return(null); } }