public void Edit(VehicleImageModel model) { var dbModel = _dbContext.Images.SingleOrDefault(x => x.Id == model.Id); if (dbModel != null && !string.IsNullOrEmpty(model.ImageFile.FileName)) { ImageHelper.DeleteFromFolder(dbModel.Path); var generatedPath = ImageHelper.SaveToFolder(model.ImageFile, EnitityTypesEnum.Vehicle, model.Id.ToString(), model.VehicleId.ToString()); dbModel.Path = generatedPath; _dbContext.SaveChanges(); } }
public void Add(VehicleImageModel model) { var generatedPath = ImageHelper.SaveToFolder(model.ImageFile, EnitityTypesEnum.Vehicle, model.Id.ToString(), model.VehicleId.ToString()); var image = new Image { Path = generatedPath }; var vehicle = _dbContext.Vehicles.SingleOrDefault(x => x.Id == model.VehicleId); var dbModel = new VehicleImage { Id = model.Id, Image = image, Vehicle = vehicle }; var createdModel = _dbContext.VehicleImages.Add(dbModel); // I know _dbContext.SaveChanges(); }