public int AddImage(HttpPostedFileBase file) { using (var context = new NonProfitContext()) { if (file != null && file.ContentLength > 0) { var path = Path.Combine("\\Uploads", Path.Combine(DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString(), file.FileName)); var newImage = new Image { ImageUrl = path }; var rootPath = HttpRuntime.AppDomainAppPath.TrimEnd('\\'); var fullPath = rootPath + path; Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); file.SaveAs(fullPath); context.Images.Add(newImage); context.SaveChanges(); return newImage.Id; } } return 0; }
public void ChangeProfilePicture(int? id, int imageId) { using (var context = new NonProfitContext()) { context.Renters.Find(id).ImageId = imageId; context.SaveChanges(); } }