public async Task <IHttpActionResult> PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employee.BusinessEntityID) { return(BadRequest()); } db.Entry(employee).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void test1() { var product = TestHelpers.CreateProduct("1"); // _context.Add(product); Assert.Equal(EntityState.Detached, _context.Entry(product).State); Assert.Empty(_context.ChangeTracker.Entries()); }
public void Test1() { Product product = _context.Product .FirstOrDefault(p => p.ProductModelId == 7); Assert.Null(product.ProductModel); _context.Entry(product).Reference(p => p.ProductModel).Load(); Assert.NotNull(product.ProductModel); }
public void ShouldEagerlyLoadRelatedData() { Product product = _context.Product.FirstOrDefault(p => p.ProductModelId == 7); Assert.Null(product.ProductModel); //you will notice that this second call calls a stored procedure //to get the data - it is also parameterized _context.Entry(product).Reference(p => p.ProductModel).Load(); Assert.NotNull(product.ProductModel); }
//ExplicitLoad public void Test3() { Product product = _context.Product .FirstOrDefault(p => p.ProductModelId == 7); _context.Entry(product).Reference(p => p.ProductModel).Load(); Assert.NotNull(product.ProductModel); //second Relation _context.Entry(product.ProductModel) .Collection(pm => pm.ProductModelIllustration).Load(); Assert.NotNull(product.ProductModel.ProductModelIllustration); }
public async Task <IActionResult> PutAddress([FromRoute] int id, [FromBody] Address address) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != address.AddressId) { return(BadRequest()); } _context.Entry(address).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AddressExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.CustomerId) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutSalesOrderHeader([FromRoute] int id, [FromBody] SalesOrderHeader salesOrderHeader) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != salesOrderHeader.SalesOrderId) { return(BadRequest()); } _context.Entry(salesOrderHeader).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SalesOrderHeaderExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProductCategory([FromRoute] int id, [FromBody] ProductCategory productCategory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != productCategory.ProductCategoryId) { return(BadRequest()); } _context.Entry(productCategory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductCategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutErrorLog([FromRoute] int id, [FromBody] ErrorLog errorLog) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != errorLog.ErrorLogId) { return(BadRequest()); } _context.Entry(errorLog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ErrorLogExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IHttpActionResult> PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductID) { 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 AddAsync(TEntity entity) { _context .Entry(entity) .State = EntityState.Added; await _context .SaveChangesAsync() .ConfigureAwait(false); }
public ActionResult Edit([Bind(Include = "DepartmentID,Name,GroupName,ModifiedDate")] Department department) { if (ModelState.IsValid) { db.Entry(department).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(department)); }
public ActionResult Edit([Bind(Include = "ProductID,Name,ProductNumber,MakeFlag,FinishedGoodsFlag,Color,SafetyStockLevel,ReorderPoint,StandardCost,ListPrice,Size,SizeUnitMeasureCode,WeightUnitMeasureCode,Weight,DaysToManufacture,ProductLine,Class,Style,ProductSubcategoryID,ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,rowguid,ModifiedDate")] Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ProductID = new SelectList(db.ProductDocuments, "ProductID", "ProductID", product.ProductID); ViewBag.ProductModelID = new SelectList(db.ProductModels, "ProductModelID", "Name", product.ProductModelID); ViewBag.ProductSubcategoryID = new SelectList(db.ProductSubcategories, "ProductSubcategoryID", "Name", product.ProductSubcategoryID); ViewBag.SizeUnitMeasureCode = new SelectList(db.UnitMeasures, "UnitMeasureCode", "Name", product.SizeUnitMeasureCode); ViewBag.WeightUnitMeasureCode = new SelectList(db.UnitMeasures, "UnitMeasureCode", "Name", product.WeightUnitMeasureCode); return(View(product)); }
public IActionResult Put([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductId) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; _context.SaveChanges(); return(Ok(product)); }
public List <Contracts.SalesOrderDetails> GetSalesOrderDetails(Int32 salesOrderID) { var details = new List <Contracts.SalesOrderDetails>(); using (var context = new AdventureWorksContext()) { details = context.SalesOrderDetails.Where(x => x.SalesOrderID == salesOrderID).ToList() .Select(x => { Product product = context.Products.FirstOrDefault(z => z.ProductID == x.ProductID); context.Entry(product).Collection(p => p.ProductProductPhotoes).Load(); var detail = Mapper.Map <SalesOrderDetail, Contracts.SalesOrderDetails>(x); Mapper.Map <Product, Contracts.SalesOrderDetails>(product, detail); return(detail); }).ToList(); } return(details); }
public void ShouldTrackChangesByDefault() { Product prod = _context.Product.First(); List <EntityEntry <Product> > entries = _context.ChangeTracker.Entries <Product>().ToList(); Assert.Single(entries); var originalName = prod.Name; prod.Name = "foo"; Assert.Equal( originalName, entries[0].OriginalValues[nameof(Product.Name)].ToString()); Assert.Equal( prod.Name, entries[0].CurrentValues[nameof(Product.Name)].ToString()); Assert.Equal(EntityState.Modified, _context.Entry(prod).State); }
public IHttpActionResult PutProduct(int id, Product product) { Log.Information("Updating product by id={ID}", id); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductID) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { Log.Error("Updating failed: product with id={Id} doesn't exist", id); return(NotFound()); } else { Log.Information("Updating failed for product by id={ID}", id); throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void ShouldNotTrackUntrackedRecords() { var product = TestHelpers.CreateProduct("1"); Assert.Equal(EntityState.Detached, _context.Entry(product).State); Assert.Empty(_context.ChangeTracker.Entries()); }
public void WhenAttachingWithNullOrDefaultPrimaryKeys() { var productAdded = TestHelpers.CreateProduct("1"); _context.Attach(productAdded); Assert.Equal(EntityState.Added, _context.Entry(productAdded).State); _context.Entry(productAdded).State = EntityState.Detached; //can attach either on the context or the DbSet _context.Product.Attach(productAdded); Assert.Equal(EntityState.Added, _context.Entry(productAdded).State); _context.Entry(productAdded).State = EntityState.Detached; _context.Product.Update(productAdded); Assert.Equal(EntityState.Added, _context.Entry(productAdded).State); _context.Entry(productAdded).State = EntityState.Detached; Assert.Throws <InvalidOperationException>(() => _context.Product.Remove(productAdded)); }