public async Task <IHttpActionResult> PutProduct(Product entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _dbContext.ApplyChanges(entity); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbContext.Products.Any(e => e.ProductId == entity.ProductId)) { return(Conflict()); } throw; } await _dbContext.LoadRelatedEntitiesAsync(entity); entity.AcceptChanges(); return(Ok(entity)); }
public async Task <IHttpActionResult> PostProduct(Product entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } entity.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(entity); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException) { if (_dbContext.Products.Any(e => e.ProductId == entity.ProductId)) { return(Conflict()); } throw; } await _dbContext.LoadRelatedEntitiesAsync(entity); entity.AcceptChanges(); return(CreatedAtRoute("DefaultApi", new { id = entity.ProductId }, entity)); }
public InternalEntitySetTests() { var products = new List <Product>(); for (int i = 0; i < NUMBER_OF_ENTITIES; i++) { var product = new Product { ProductID = i, ProductName = "Prod #" + i }; product.AcceptChanges(); products.Add(product); } _products = products.ToArray(); }
public void CRUDProduct() { // Insert var product = new Product { }; Assert.IsTrue(!product.IsValid && !string.IsNullOrEmpty(product.ValidationMessage) && product.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_EmptyName"))); Assert.IsTrue(!product.IsValid && !string.IsNullOrEmpty(product.ValidationMessage) && product.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_EmptyCreatedBy"))); product.Name = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; product.CreatedBy = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"; Assert.IsTrue(!product.IsValid && !string.IsNullOrEmpty(product.ValidationMessage) && product.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_MaxNameLength"))); Assert.IsTrue(!product.IsValid && !string.IsNullOrEmpty(product.ValidationMessage) && product.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_MaxCreatedByLength"))); product.Name = "New temporal product"; product.CreatedBy = "dcruz"; Assert.IsTrue(product.IsValid); Assert.IsTrue(product.IsNew); product.AcceptChanges(); Assert.IsTrue(product.IsValid); Assert.IsTrue(!product.IsChanged); var loaded = Product.Load(product.ID); Assert.IsNotNull(loaded); Assert.IsTrue(!loaded.IsChanged); Assert.IsTrue(!loaded.IsNew); Compare(loaded, product); // Update loaded.Name = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; Assert.IsTrue(!loaded.IsValid && !string.IsNullOrEmpty(loaded.ValidationMessage) && loaded.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_MaxNameLength"))); Assert.IsTrue(!loaded.IsValid && !string.IsNullOrEmpty(loaded.ValidationMessage) && loaded.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_EmptyLastUpdatedBy"))); var any = Product.GetAll().Where(m => m.ID != loaded.ID).FirstOrDefault(); if (any == null) Assert.Inconclusive(); loaded.Name = any.Name; Assert.IsTrue(!loaded.IsValid && !string.IsNullOrEmpty(loaded.ValidationMessage) && loaded.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_DuplicatedName", new { loaded.Name }))); Assert.IsTrue(!loaded.IsValid && !string.IsNullOrEmpty(loaded.ValidationMessage) && loaded.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_EmptyLastUpdatedBy"))); loaded.Name = "New temporal product 2"; loaded.LastUpdatedBy = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"; Assert.IsTrue(!loaded.IsValid && !string.IsNullOrEmpty(loaded.ValidationMessage) && loaded.ValidationMessage.Contains(ResourceStringLoader.GetResourceString("Product_MaxLastUpdatedByLength"))); loaded.LastUpdatedBy = "dcruz"; Assert.IsTrue(loaded.IsChanged); Assert.IsTrue(loaded.IsValid); loaded.AcceptChanges(); Assert.IsTrue(!loaded.IsChanged); Assert.IsTrue(loaded.IsValid); var updated = Product.Load(product.ID); Assert.IsNotNull(updated); Compare(updated, loaded); // Delete loaded = Product.Load(product.ID); loaded.Delete(); Assert.IsTrue(loaded.IsValid); Assert.IsTrue(loaded.IsDeleted); loaded.AcceptChanges(); loaded = Product.Load(product.ID); Assert.IsNull(loaded); }
private Product AttachWithRelations(Product entity, InsertMode insertMode = InsertMode.Attach, MergeOption mergeOption = MergeOption.AppendOnly, List <object> referenceTrackingList = null) { #region iteration tracking if (ReferenceEquals(null, referenceTrackingList)) { referenceTrackingList = new List <object>(); } if (referenceTrackingList.Contains(entity)) { return(_products.GetExisting(entity)); } else { referenceTrackingList.Add(entity); } #endregion #region add/attach entity Product existingEntity = null; switch (insertMode) { case InsertMode.Add: existingEntity = _products.Add(entity); break; case InsertMode.Attach: existingEntity = _products.Attach(entity); break; default: throw new Exception(string.Format("Implementation Exception: missing action for {0}", insertMode)); } if (!ReferenceEquals(null, existingEntity) && ReferenceEquals(existingEntity, entity)) { return(existingEntity); } #endregion #region attach relations recursively // register entity's property changed event if entity is new to context if (ReferenceEquals(null, existingEntity)) { entity.PropertyChanged += On_product_propertyChanged; } // attach related entity to context if (!ReferenceEquals(null, entity.ProductCategory)) { var existingRelatedEntity = AttachWithRelations(entity.ProductCategory, insertMode, mergeOption, referenceTrackingList); } #endregion #region refresh existing entity based on merge options if (!ReferenceEquals(null, existingEntity) && !ReferenceEquals(existingEntity, entity)) { if (Products.MergeOption == MergeOption.OverwriteChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false); existingEntity.AcceptChanges(); }); } else if (Products.MergeOption == MergeOption.PreserveChanges) { Invoke(delegate { existingEntity.Refresh(entity, trackChanges: false, preserveExistingChanges: true); }); } } #endregion return(existingEntity); }