public IActionResult UploadPhoto(int albumId, IList <IFormFile> files) { if (ModelState.IsValid) { var album = repository.Albums.FirstOrDefault(a => a.AlbumID == albumId); if (album != null) { if (album.Lines == null) { album.Lines = new List <AlbumPictureLine>(); } } else { //todo:log error } if (files != null && files.Count() > 0) { var filePath = hostingEnv.WebRootPath + $@"\Files\Pictures\Albums\{album.Name}\"; foreach (var file in files) { var pictureName = SavePicture(file, filePath);//保存上送的图片 if (!string.IsNullOrEmpty(pictureName)) { var picture = new Picture(); picture.PictureName = $"/Albums/{album.Name}/{pictureName}"; picture.CreateTime = DateTime.Now; context.Pictures.Add(picture); album.Lines.Add(new AlbumPictureLine() { Picture = picture }); repository.SaveAlbum(album); } } } else { TempData["message"] = $"上传张照片到{album.Name}失败!"; return(View(album)); } context.SaveChanges(); TempData["message"] = $"成功上传 {files.Count()}张照片到{album.Name}"; return(View(album)); } else { // there is something wrong with the data values return(RedirectToAction("List")); } }
public void SaveDish(Dish dish) { if (dish.DishID == 0) { context.Dishes.Add(dish); } else { Dish dbEntry = context.Dishes.FirstOrDefault(p => p.DishID == dish.DishID); if (dbEntry != null) { dbEntry.Name = dish.Name; dbEntry.Description = dish.Description; //dbEntry.Price = dish.Price; dbEntry.Category = dish.Category; dbEntry.ImageName = dish.ImageName; dbEntry.Remark = dish.Remark; dbEntry.Disabled = dish.Disabled; } } context.SaveChanges(); }
public void SaveAlbum(Album album) { //if(album.Lines != null) // context.AttachRange(album.Lines.Select(a => a.Picture)); if (album.AlbumID == 0) { context.Albums.Add(album); } else { Album dbEntry = context.Albums.FirstOrDefault(a => a.AlbumID == album.AlbumID); if (dbEntry != null) { dbEntry.Name = album.Name; dbEntry.Description = album.Description; dbEntry.Category = album.Category; dbEntry.CreateTime = album.CreateTime; dbEntry.Disabled = album.Disabled; dbEntry.Lines = album.Lines; } } context.SaveChanges(); }
public static void EnsurePopulated(IApplicationBuilder app) { //ApplicationDbContext context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>(); //if (!context.Products.Any()) //{ // context.Products.AddRange( // new Product { Name = "Kayak", Description = "A boat for one person", Category = "Watersports", Price = 275 }, // new Product { Name = "Lifejacket", Description = "Protective and fashionable", Category = "Watersports", Price = 48.95m }, // new Product { Name = "Soccer Ball", Description = "FIFA-approved size and weight", Category = "Soccer", Price = 19.50m }, // new Product { Name = "Corner Flags", Description = "Give your playing field a professional touch", Category = "Soccer", Price = 34.95m }, // new Product { Name = "Stadium", Description = "Flat-packed 35,000-seat stadium", Category = "Soccer", Price = 79500 }, // new Product { Name = "Thinking Cap", Description = "Improve brain efficiency by 75%", Category = "Chess", Price = 16 }, // new Product { Name = "Unsteady Chair", Description = "Secretly give your opponent a disadvantage", Category = "Chess", Price = 29.95m }, // new Product { Name = "Human Chess Board", Description = "A fun game for the family", Category = "Chess", Price = 75 }, // new Product { Name = "Bling-Bling King", Description = "Gold-plated, diamond-studded King", Category = "Chess", Price = 1200 }); context.SaveChanges(); //} #region 车票预定 //BusTicketDbContext busTicketDbContext = app.ApplicationServices.GetRequiredService<BusTicketDbContext>(); //if (!busTicketDbContext.Tickets.Any()) //{ // busTicketDbContext.Tickets.AddRange( // new Ticket { StartStation = "富士康", TerminalStation = "平顶山服务区", Price = 40, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "叶县", Price = 40, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "旧县", Price = 40, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "方城", Price = 50, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "石桥", Price = 50, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "南阳", Price = 50, Description = "备注信息", Category = "富士康专线" }, // new Ticket { StartStation = "富士康", TerminalStation = "镇平", Price = 60, Description = "备注信息", Category = "富士康专线" } // ); // busTicketDbContext.SaveChanges(); //} #endregion SuperHuiDbContext SuperHuiContext = app.ApplicationServices.GetRequiredService <SuperHuiDbContext>(); if (!SuperHuiContext.Dishes.Any()) { SuperHuiContext.Dishes.AddRange( new Dish { Name = "酱焖嘎鱼", Description = "肉质鲜嫩", Category = "鱼&生鲜", ImageName = "gayu.jpg" }, new Dish { Name = "糖醋鱼", Description = "武昌鱼、罗菲鱼", Category = "鱼&生鲜", ImageName = "suantaichaorou.jpg" }, new Dish { Name = "小葱拌豆腐", Description = "", Category = "凉菜", ImageName = "hds.jpg" }, new Dish { Name = "菠菜花生", Description = "", Category = "凉菜", ImageName = "lianou.jpg" }, new Dish { Name = "拍黄瓜", Description = "", Category = "凉菜", ImageName = "hongshaorou.jpg" } ); SuperHuiContext.SaveChanges(); } if (!SuperHuiContext.Albums.Any()) { SuperHuiContext.Albums.AddRange( new Album { Name = "泰山行", Description = "夜爬泰山,别有一番风景", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "紫竹院公园", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "奥森公园", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "凤凰岭", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "香山", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "长城", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "古北水镇", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "玉渊潭公园", Description = "", Category = "旅行", CreateTime = System.DateTime.Now }, new Album { Name = "十渡漂流", Description = "", Category = "旅行", CreateTime = System.DateTime.Now } ); SuperHuiContext.SaveChanges(); } }