コード例 #1
0
    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);
            }
        }
    }
コード例 #2
0
    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);
                }
            }
    }
コード例 #3
0
        // 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"));
                }
            }
        }
コード例 #4
0
    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);
        }
    }
コード例 #5
0
        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"));
            }
        }