public async Task Update_UpdateRecord_ValidateResult() { using (SqlXRepository repository = new SqlXRepository(_connectionString)) { var newX = new EntityX { Name = "Update1", Price = 1 }; try { await repository.CreateAsync(newX); newX = await repository.ReadAsync(newX.Id); newX.Name = "Update2"; newX.Price = 2; await repository.UpdateAsync(newX); var readX = await repository.ReadAsync(newX.Id); Assert.AreEqual(newX.Name, readX.Name); Assert.AreEqual(newX.Id, readX.Id); Assert.AreEqual(newX.Price, readX.Price); Assert.IsFalse(newX.RowVersion == readX.RowVersion); } finally { await repository.DeleteAsync(newX.Id); } } }
public async Task GetList_ReadAllRecords_RecordsFound() { using (SqlXRepository repository = new SqlXRepository(_connectionString)) { var items = (await repository.GetListAsync()).ToList(); Assert.IsTrue(items.Count > 0); } }
// GET: X public async Task <ActionResult> Index() { ViewBag.Message = TempData["Message"]; TempData["Message"] = string.Empty; using (SqlXRepository repository = new SqlXRepository()) { var items = (await repository.GetListAsync()).ToList(); return(View(items)); } }
public async Task <ActionResult> Create(EntityX model) { try { using (SqlXRepository repository = new SqlXRepository()) { await repository.CreateAsync(model).ConfigureAwait(false); } TempData["Message"] = $"{model.Name} created."; return(RedirectToAction("Index")); } catch { return(View()); } }
// GET: X/Edit/5 public async Task <ActionResult> Edit(int id) { using (SqlXRepository repository = new SqlXRepository()) { try { var model = await repository.ReadAsync(id).ConfigureAwait(false); return(View(model)); } catch (Exception ex) { TempData["Message"] = $"There was an error updating record: {id}"; return(RedirectToAction("Index")); } } }
// GET: X/Delete/5 public async Task <ActionResult> Delete(int id) { using (SqlXRepository repository = new SqlXRepository()) { try { await repository.DeleteAsync(id).ConfigureAwait(false); TempData["Message"] = "Record deleted successfully."; } catch (Exception ex) { TempData["Message"] = $"There was an error deleting record: {id}"; } } return(RedirectToAction("Index")); }
public async Task Update_UpdateRecordParallelOptimistic_ValidateResult() { var newX = new EntityX { Name = "Update1", Price = 1 }; using (SqlXRepository repository1 = new SqlXRepository(_connectionString)) using (SqlXRepository repository2 = new SqlXRepository(_connectionString)) { try { await repository1.CreateAsync(newX); //Read Item by 1-st user newX = await repository1.ReadAsync(newX.Id); //Read the same item by 2-nd user var readX2 = await repository2.ReadAsync(newX.Id); //Modify and save updated by first user newX.Name = "Update2"; newX.Price = 2; await repository1.UpdateAsync(newX); //try to update the same record by 2-nd user readX2.Name = "Update3"; readX2.Price = 3; //update should fail here try { await repository2.UpdateAsync(readX2); } catch (ConcurrencyException ex) { Assert.IsTrue(ex.Message == "The value was already changed"); } catch { Assert.Fail(); } //the result should be from the 1-st user update var readX = await repository1.ReadAsync(newX.Id); Assert.AreEqual(newX.Name, readX.Name); Assert.AreEqual(newX.Id, readX.Id); Assert.AreEqual(newX.Price, readX.Price); Assert.IsFalse(newX.RowVersion == readX.RowVersion); } finally { await repository1.DeleteAsync(newX.Id); } } }
public async Task CreateListRemove_Crud_Validate() { using (SqlXRepository repository = new SqlXRepository(_connectionString)) { var newX = new EntityX { Name = "Test", Price = 1 }; await repository.CreateAsync(newX); Assert.IsTrue(newX.Id > 0); var readX = await repository.ReadAsync(newX.Id); Assert.AreEqual(newX.Name, readX.Name); Assert.AreEqual(newX.Id, readX.Id); Assert.AreEqual(newX.Price, readX.Price); Assert.IsFalse(readX.RowVersion == Guid.Empty); await repository.DeleteAsync(newX.Id); } }
public async Task <ActionResult> Edit(EntityX model) { try { using (SqlXRepository repository = new SqlXRepository()) { try { await repository.UpdateAsync(model).ConfigureAwait(false); TempData["Message"] = $"{model.Name} updated successfully."; } catch (ConcurrencyException ex) { var merge = new MergeEntityX { NewItem = model, SavedItem = await repository.ReadAsync(model.Id), }; return(View("EditMerge", merge)); } catch (Exception ex) { TempData["Message"] = $"There was an error updating record: {model.Id}"; } } return(RedirectToAction("Index")); } catch (Exception ex) { TempData["Message"] = $"Cannot update {model.Name}."; return(RedirectToAction("Index")); } }