static void Main(string[] args) { var ctx = new CatalogContext(); var course1 = new Course { Title = "new course", Prereq = "Nothing" }; Console.WriteLine(ctx.Entry(course1).State); var course = (from c in ctx.Courses where c.Title == "JAVA EE" select c).FirstOrDefault(); Console.WriteLine(ctx.Entry(course).State); // Unchanged if (course != null) { course.Prereq = "Java + SQL + HTML"; // Modified Console.WriteLine(ctx.Entry(course).State); ctx.SaveChanges(); } else { Console.WriteLine("Course Not Found!"); } }
public static GameCopy LoadGame(CatalogContext database, int gameCopyId) { var game = database.Games.Find(gameCopyId); if (game == null) { // TODO: Create an exception for this. throw new Exception($"Game with ID {gameCopyId} not found."); } var entry = database.Entry(game); entry .Collection(v => v.Items) .Query() .Include(item => item.Files) .Include(item => item.Scans) .Load(); entry .Collection(v => v.GameCopyDevelopers) .Query() .Include(gcd => gcd.Developer) .Load(); entry .Reference(v => v.Publisher) .Load(); return(game); }
public async Task <IHttpActionResult> PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ID) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutModel(int id, Model model) { if (id != model.Id) { return(BadRequest()); } _context.Entry(model).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ModelExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProductCategory(int id, ProductCategory productCategory) { if (id != productCategory.Id) { return(BadRequest()); } _context.Entry(productCategory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductCategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Save(CodelistEditModel model) { var typeName = model.Name; var type = _types.Where(t => t.Name == typeName).SingleOrDefault(); if (type != null) { using (CatalogContext db = new CatalogContext()) { var keys = new object[] { model.ID }; var res = (ICodelist)db.Set(type).Find(keys); if (res != null) { res.ShortDescription = model.ShortDescription; res.LongDescription = model.LongDescription; res.CodelistNumber = model.CodelistNumber; res.SortOrder = model.SortOrder; db.Entry(res).State = EntityState.Modified; db.SaveChanges(); } } } return(Redirect("~/Codelists?n=" + model.Name)); }
public async Task <Item> GetAsync(Guid id) { var item = await _context.Items .AsNoTracking() .Where(x => x.Id == id) .Include(x => x.Genre) .Include(x => x.Artist).FirstOrDefaultAsync(); if (item == null) { return(null); } _context.Entry(item).State = EntityState.Detached; return(item); }
private static GameCopy LoadGame(CatalogContext database, int gameCopyId) { var gameCopy = database.Games.Find(gameCopyId); var entry = database.Entry(gameCopy); entry .Collection(g => g.Items) .Query() .Include(item => item.Files) .Include(item => item.Scans) .Load(); entry .Collection(g => g.GameCopyDevelopers) .Load(); entry .Collection(g => g.GameCopyTags) .Load(); entry .Reference(g => g.Publisher) .Load(); return(gameCopy); }
public async Task UpdateAsync(T entity) { var originalProduct = await GetByIdAsync(entity.Id); _dbContext.Entry(originalProduct).CurrentValues.SetValues(entity); await _dbContext.SaveChangesAsync(); }
private static void TestEFProduct() { using (var context = new CatalogContext()) { context.ProductGroups.Add(ProductGroup.Create("Rosa")); context.ProductGroups.Add(ProductGroup.Create("Gerbera")); context.ProductGroups.Add(ProductGroup.Create("Tulip")); context.SaveChanges(); var rosa = context.ProductGroups.Where(pg => pg.Name == "Rosa").SingleOrDefault(); var rosaId = context.Entry(rosa).Property("ProductGroupId").CurrentValue; var tulip = context.ProductGroups.Where(pg => Microsoft.EntityFrameworkCore.EF.Property <int>(pg, "ProductGroupId") == 3).SingleOrDefault(); var redBerlin = Product.Create(rosa, "R GR Red Berlin", null, "R GR Red Berlin"); var ajax = Product.Create(tulip, "TU EN AJAX", null, "TU EN AJAX"); context.Products.Add(redBerlin); context.Products.Add(ajax); context.SaveChanges(); } var productGroups = CatalogQueries.GetProductGroups(); }
public bool UpdateAnimal(M.Animal animal) { bool animalUpdated = false; try { M.Animal matchedAnimal = _dbContext.Animals.SingleOrDefault(a => a.Id == animal.Id); if (matchedAnimal != null) { // Detach first entity (there cannot be two entities with same id being searched in the DB at the same time) _dbContext.Entry(matchedAnimal).State = EntityState.Detached; _dbContext.Animals.Update(animal); _dbContext.SaveChanges(); animalUpdated = true; } else { Debug.WriteLine("Animal not found!"); } } catch (Exception err) { Debug.WriteLine(err.Message); } return(animalUpdated); }
public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Item> GetAsync(Guid id) { var item = await _context.Items.FindAsync(id); if (item == null) { return(null); } await _context.Entry(item) .Reference(i => i.Artist).LoadAsync(); await _context.Entry(item) .Reference(i => i.Genre).LoadAsync(); return(item); }
/// <summary> /// Updates an existing item in items table in database /// </summary> /// <param name="item"></param> /// <returns></returns> public Item Update(Item item) { /* returns the entry of the passed item entity * and change the state to modified sothat being effectively * saved by UnitOfWork */ _context.Entry(item).State = EntityState.Modified; return(item); }
public ActionResult Edit([Bind(Include = "ID,PeriodNumber")] Period period) { if (ModelState.IsValid) { db.Entry(period).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(period)); }
public ActionResult Edit(User user, int[] selectedProfiles) { User newUser = db.Users.Find(user.Id); newUser.Name = user.Name; newUser.Login = user.Login; newUser.Profiles.Clear(); if (selectedProfiles != null) { //получаем выбранные профили foreach (var c in db.Profiles.Where(co => selectedProfiles.Contains(co.Id))) { newUser.Profiles.Add(c); } } db.Entry(newUser).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(Catalog catalog) { if (ModelState.IsValid) { db.Entry(catalog).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(catalog)); }
public ActionResult Edit([Bind(Include = "Id,Name,Description")] Role role) { if (ModelState.IsValid) { db.Entry(role).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(role); }
public ActionResult EditProfile(Profile profile, int[] selectedRoles) { Profile newProfile = db.Profiles.Find(profile.Id); newProfile.Name = profile.Name; newProfile.Roles.Clear(); if (selectedRoles != null) { //получаем выбранные роли foreach (var c in db.Roles.Where(co => selectedRoles.Contains(co.Id))) { newProfile.Roles.Add(c); } } db.Entry(newProfile).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("onProfile")); }
public ActionResult Edit([Bind(Include = "TagId,TagName")] Tag tag) { if (ModelState.IsValid) { db.Entry(tag).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tag)); }
public void InsertOrUpdate(Category category) { if (category.CategoryID == default(int)) { context.Categories.Add(category); } else { context.Entry(category).State = EntityState.Modified; } }
public ActionResult Edit([Bind(Include = "ID,Name,Instructor,Description,Capacity,CurrentStudentCount,PeriodID")] Elective elective) { if (ModelState.IsValid) { db.Entry(elective).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.PeriodID = new SelectList(db.Periods, "ID", "ID", elective.PeriodID); return(View(elective)); }
public void InsertOrUpdate(Product product) { if (product.ProductID == default(int)) { context.Products.Add(product); } else { context.Entry(product).State = EntityState.Modified; } }
public async Task <ActionResult <EventItem> > UpdateEvent(int id, EventItem eventItem) { if (id != eventItem.Id) { return(BadRequest()); } _context.Entry(eventItem).State = EntityState.Modified; _context.SaveChanges(); return(NoContent()); }
public async Task <Artist> GetAsync(Guid id) { var artist = await _catalogContext.Artists .FindAsync(id); if (artist == null) { return(null); } _catalogContext.Entry(artist).State = EntityState.Detached; return(artist); }
public async Task <Genre> GetAsync(Guid id) { var item = await _catalogContext.Genres .FindAsync(id); if (item == null) { return(null); } _catalogContext.Entry(item).State = EntityState.Detached; return(item); }
public async Task <IActionResult> UpdateProduct(int id, Product product) { if (id != product.Id) { _logger.LogError($"Id {id} does not match product id {product.Id}"); return(BadRequest()); } var productFromDb = await _context.Products.FindAsync(id); if (productFromDb is null) { _logger.LogError($"Product with id {id} not found."); return(NotFound()); } _context.Entry(productFromDb).CurrentValues.SetValues(product); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> PutCatalogItem(int id, CatalogItem productToUpdate) { if (id != productToUpdate.Id) { return(BadRequest()); } try { var catalogItem = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id); var oldPrice = catalogItem.Price; var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price; if (raiseProductPriceChangedEvent) { var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice); catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent); } _context.Entry(productToUpdate).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CatalogItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public virtual async Task <Product> UpdateProductAsync(Product product, int id) { if (product == null) { return(null); } var exist = await _db.Product.FindAsync(id); if (exist != null) { exist.IdProduct = id; exist.ModifiedDate = DateTime.Now; exist.CategoryId = product.CategoryId; exist.ShortDescription = product.ShortDescription; exist.UnitPrice = product.UnitPrice; exist.UnitStock = product.UnitStock; _db.Entry(exist).State = EntityState.Modified; await _db.SaveChangesAsync(); return(exist); } return(null); }
public async Task UpdateAsync(TEntity entity) { _dbContext.Entry(entity).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }