public static void AttachToContext(UserProfile profile, SDCContext db) { if (db.Set <UserProfile>().Local.Any(local => profile == local)) { db.Entry <UserProfile>(profile).State = EntityState.Unchanged; } else { db.Set <UserProfile>().Attach(profile); db.Entry <UserProfile>(profile).State = EntityState.Unchanged; } }
public async Task <IActionResult> PutProduct([FromRoute] string productId, int supplierId, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (productId != product.ProductId) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(productId) && !SupplierExists(supplierId)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutInvoice([FromRoute] int id, [FromBody] Invoice invoice) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != invoice.ProjectId) { return(BadRequest()); } _context.Entry(invoice).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InvoiceExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutItems([FromRoute] int projectId, string roomId, string productId, [FromBody] Items items) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (projectId != items.ProjectId) { return(BadRequest()); } _context.Entry(items).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProjectExists(projectId) && !RoomExists(roomId) && !ProductExists(productId)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCodeCategory([FromRoute] int id, [FromBody] CodeCategory codeCategory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != codeCategory.CategoryId) { return(BadRequest()); } _context.Entry(codeCategory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CodeCategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutSupplierOrder([FromRoute] int id, [FromBody] SupplierOrder supplierOrder) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != supplierOrder.SupplierId) { return(BadRequest()); } _context.Entry(supplierOrder).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SupplierOrderExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task<IActionResult> PutRoom([FromRoute] string roomId, int projectId, [FromBody] Room room) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (roomId != room.RoomId) { return BadRequest(); } _context.Entry(room).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RoomExists(roomId) && !ProjectExists(projectId)) { return NotFound(); } else { throw; } } return NoContent(); }
public async Task <IActionResult> PutPurchaseOrderdetail([FromRoute] int projectId, int orderId, string productId, [FromBody] PurchaseOrderdetail purchaseOrderdetail) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (projectId != purchaseOrderdetail.ProjectId) { return(BadRequest()); } _context.Entry(purchaseOrderdetail).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProjectExists(projectId) && !OrderExists(orderId) && !ProductExists(productId)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public static void MapComplexProperties(SDCContext db, Book book, BookViewModel bookViewModel, UserProfile profile) { #region Authors entities var auth_to_remove = book.Authors.Where(a => !bookViewModel.Authors.Any(a2 => a2.Id == a.Id)).ToList(); var auth_to_add = bookViewModel.Authors.Where(a => !book.Authors.Any(a2 => a2.Id == a.Id)).ToList(); auth_to_remove.ForEach(a => book.Authors.Remove(a)); auth_to_add.ForEach(a => book.Authors.Add(a)); foreach (Author a in book.Authors) { if (a.Id == 0) { a.AddedDate = DateTime.Now; a.AddedBy = profile; db.Entry <Author>(a).State = EntityState.Added; } else { db.Attach(a); } } #endregion #region Genres entities var genres_to_remove = book.Genres.Where(g => !bookViewModel.Genres.Any(g2 => g2.Id == g.Id)).ToList(); var genres_to_add = bookViewModel.Genres.Where(g => !book.Genres.Any(g2 => g2.Id == g.Id)).ToList(); genres_to_remove.ForEach(g => book.Genres.Remove(g)); genres_to_add.ForEach(g => book.Genres.Add(g)); foreach (var g in book.Genres) { db.Attach(g); } #endregion #region Publisher entity if (bookViewModel.Publisher != null) { db.Attach(bookViewModel.Publisher); book.Publisher = bookViewModel.Publisher; } else { book.Publisher = null; } #endregion #region Language var lang = bookViewModel.Language; db.AttachCodeEntity(ref lang); book.Language = lang; #endregion }
public ActionResult Index() { ViewBag.Breadcrumbs = Breadcrumb.Generate( "My shelves", ""); var userProfile = (UserProfile)this.Session["UserInfo"]; if (userProfile == null) { return(RedirectToAction("Index", "Home")); } using (var db = new SDCContext()) { var shelves = (from s in db.Shelves orderby s.Name where s.Owner.UserId == userProfile.UserId select s).ToList(); var shelvesVMs = shelves.Select(s => { return(new ShelfViewModel() { Id = s.Id, Name = s.Name, CreationDate = s.CreationDate, IsVisible = s.IsVisible, BookCount = db.Entry(s).Collection(p => p.Books).Query().Count() }); }).ToArray(); return(View(new ShelvesViewModel() { Shelves = shelvesVMs })); } }
public Task <int> Import(int max = 0) { if (!_csvDataLoaded) { throw new Exception("CSV data not loaded."); } if (max == 0) { _max = TotalBookCount; } else { _max = max; } Task <int> importTask = new Task <int>(() => { try { Progress = 0; Running = true; Cancel = false; ImportStart = DateTime.Now; TargetCount = _max; using (var db = new SDCContext()) { db.Configuration.AutoDetectChangesEnabled = false; _allGenres = db.Genres.ToArray(); _lang = db.Languages.Find("FR"); _country = db.Countries.Find("CA"); _city = db.Cities.Find(4); var authors = db.Authors.ToList(); authors.ForEach(a => { if (!_authorsSet.ContainsKey(a.Name)) { _authorsSet.Add(a.Name, a); } }); var publishers = db.Publishers.ToList(); publishers.ForEach(p => { if (!_publishersSet.ContainsKey(p.Name)) { _publishersSet.Add(p.Name, p); } }); do { string firstName = _firstNames[_rnd.Next(0, _firstNames.Length - 1)]; string lastName = _lastNames[_rnd.Next(0, _lastNames.Length - 1)]; Shelf shelf; var profile = CreateUser(db, firstName, lastName, out shelf); LoadBooks(db, profile, shelf); db.ChangeTracker.DetectChanges(); db.SaveChanges(); var localBooks = db.Books.Local.ToArray(); foreach (var le in localBooks) { db.Entry(le).State = System.Data.Entity.EntityState.Detached; } var localPictures = db.BookPictures.Local.ToArray(); foreach (var le in localPictures) { db.Entry(le).State = System.Data.Entity.EntityState.Detached; } } while (Progress < _max && !Cancel); } return(Progress); } finally { Running = false; } }); importTask.Start(); return(importTask); }
public static void AttachToContext(UserProfile profile, SDCContext db) { if (db.Set<UserProfile>().Local.Any(local => profile == local)) { db.Entry<UserProfile>(profile).State = EntityState.Unchanged; } else { db.Set<UserProfile>().Attach(profile); db.Entry<UserProfile>(profile).State = EntityState.Unchanged; } }
public ActionResult Index() { ViewBag.Breadcrumbs = Breadcrumb.Generate( "My shelves", ""); var userProfile = (UserProfile)this.Session["UserInfo"]; if(userProfile == null) { return RedirectToAction("Index", "Home"); } using (var db = new SDCContext()) { var shelves = (from s in db.Shelves orderby s.Name where s.Owner.UserId == userProfile.UserId select s).ToList(); var shelvesVMs = shelves.Select(s => { return new ShelfViewModel() { Id = s.Id, Name = s.Name, CreationDate = s.CreationDate, IsVisible = s.IsVisible, BookCount = db.Entry(s).Collection(p=>p.Books).Query().Count() }; }).ToArray(); return View(new ShelvesViewModel() { Shelves = shelvesVMs }); } }
public Task<int> Import(int max = 0) { if (!_csvDataLoaded) throw new Exception("CSV data not loaded."); if (max == 0) _max = TotalBookCount; else _max = max; Task<int> importTask = new Task<int>(() => { try { Progress = 0; Running = true; Cancel = false; ImportStart = DateTime.Now; TargetCount = _max; using (var db = new SDCContext()) { db.Configuration.AutoDetectChangesEnabled = false; _allGenres = db.Genres.ToArray(); _lang = db.Languages.Find("FR"); _country = db.Countries.Find("CA"); _city = db.Cities.Find(4); var authors = db.Authors.ToList(); authors.ForEach(a => { if (!_authorsSet.ContainsKey(a.Name)) _authorsSet.Add(a.Name, a); }); var publishers = db.Publishers.ToList(); publishers.ForEach(p => { if (!_publishersSet.ContainsKey(p.Name)) _publishersSet.Add(p.Name, p); }); do { string firstName = _firstNames[_rnd.Next(0, _firstNames.Length - 1)]; string lastName = _lastNames[_rnd.Next(0, _lastNames.Length - 1)]; Shelf shelf; var profile = CreateUser(db, firstName, lastName, out shelf); LoadBooks(db, profile, shelf); db.ChangeTracker.DetectChanges(); db.SaveChanges(); var localBooks = db.Books.Local.ToArray(); foreach(var le in localBooks) { db.Entry(le).State = System.Data.Entity.EntityState.Detached; } var localPictures = db.BookPictures.Local.ToArray(); foreach(var le in localPictures) { db.Entry(le).State = System.Data.Entity.EntityState.Detached; } } while (Progress < _max && !Cancel); } return Progress; } finally { Running = false; } }); importTask.Start(); return importTask; }
public static void MapComplexProperties(SDCContext db, Book book, BookViewModel bookViewModel, UserProfile profile) { #region Authors entities var auth_to_remove = book.Authors.Where(a => !bookViewModel.Authors.Any(a2 => a2.Id == a.Id)).ToList(); var auth_to_add = bookViewModel.Authors.Where(a => !book.Authors.Any(a2 => a2.Id == a.Id)).ToList(); auth_to_remove.ForEach(a => book.Authors.Remove(a)); auth_to_add.ForEach(a => book.Authors.Add(a)); foreach (Author a in book.Authors) { if (a.Id == 0) { a.AddedDate = DateTime.Now; a.AddedBy = profile; db.Entry<Author>(a).State = EntityState.Added; } else { db.Attach(a); } } #endregion #region Genres entities var genres_to_remove = book.Genres.Where(g => !bookViewModel.Genres.Any(g2 => g2.Id == g.Id)).ToList(); var genres_to_add = bookViewModel.Genres.Where(g => !book.Genres.Any(g2 => g2.Id == g.Id)).ToList(); genres_to_remove.ForEach(g => book.Genres.Remove(g)); genres_to_add.ForEach(g => book.Genres.Add(g)); foreach (var g in book.Genres) { db.Attach(g); } #endregion #region Publisher entity if (bookViewModel.Publisher != null) { db.Attach(bookViewModel.Publisher); book.Publisher = bookViewModel.Publisher; } else book.Publisher = null; #endregion #region Language var lang = bookViewModel.Language; db.AttachCodeEntity(ref lang); book.Language = lang; #endregion }