public async Task Update_UpdateRecord_ValidateResult()
    {
        using (SqlYRepository repository = new SqlYRepository(_connectionString)) {
            var newY = new EntityY {
                Name  = "Update1",
                Price = 1
            };
            try {
                await repository.CreateAsync(newY);

                newY = await repository.ReadAsync(newY.Id, "user");

                newY.Name  = "Update2";
                newY.Price = 2;
                await repository.UpdateAsync(newY);

                var readX = await repository.ReadAsync(newY.Id, "user");

                Assert.AreEqual(newY.Name, readX.Name);
                Assert.AreEqual(newY.Id, readX.Id);
                Assert.AreEqual(newY.Price, readX.Price);
            } finally {
                await repository.DeleteAsync(newY.Id, "user");
            }
        }
    }
Esempio n. 2
0
 public async Task <ActionResult> Create(EntityY model)
 {
     try {
         using (var repository = new SqlYRepository()) {
             await repository.CreateAsync(model).ConfigureAwait(false);
         }
         TempData["Message"] = $"{model.Name} created.";
         return(RedirectToAction("Index"));
     } catch {
         return(View());
     }
 }
    public async Task Update_UpdateRecordParallelPessimistic_ValidateResult()
    {
        var newY = new EntityY {
            Name  = "Update1",
            Price = 1
        };

        using (SqlYRepository repository1 = new SqlYRepository(_connectionString))
            using (SqlYRepository repository2 = new SqlYRepository(_connectionString)) {
                try {
                    await repository1.CreateAsync(newY);

                    //Read Item by 1-st user
                    newY = await repository1.ReadAsync(newY.Id, "user1");

                    //read should fail here
                    EntityY readY2 = null;
                    try {
                        readY2 = await repository2.ReadAsync(newY.Id, "user2");
                    } catch (ConcurrencyException ex) {
                        Assert.IsTrue(ex.Message.StartsWith("Lock already set"));
                    } catch (Exception ex) {
                        Assert.Fail();
                    }
                    //Read the same item by 2-nd user, should fail here


                    //Modify and save updated by first user
                    newY.Name     = "Update2";
                    newY.Price    = 2;
                    newY.LockedBy = "user1";
                    await repository1.UpdateAsync(newY);

                    //the result should be from the 1-st user update
                    var readY = await repository1.ReadAsync(newY.Id, "user1");

                    Assert.AreEqual(newY.Name, readY.Name);
                    Assert.AreEqual(newY.Id, readY.Id);
                    Assert.AreEqual(newY.Price, readY.Price);
                } finally {
                    await repository1.DeleteAsync(newY.Id, "user1");
                }
            }
    }
Esempio n. 4
0
        public void SharedEntities_AccessSharedInheritance()
        {
            this.EnqueueLoadEntityX();

            this.EnqueueCallback(
                () =>
            {
                EntityX x = (EntityY)this.DomainChildContext.EntityXes.First();
                EntityY y = (EntityY)x;
                Assert.IsNotNull(y.EntityZ, "y.EntityZ should be set");
                Assert.AreEqual(y.IdZ, y.EntityZ.Id, "y.IdZ should reflect EntityZ.Id");
                Assert.AreNotEqual(y.IdZ, 0, "y.IdZ should not be 0");
                // When Y is serialized DCS serializes X' properties then Y's properties.
                // If the surrogates were loaded correctly, X' ZProp should be read before Y's IdZ.
                // If surrogates were not loaded correctly, ZProp would not be set.
                Assert.AreNotEqual(x.ZProp, 0, "x.ZProp cannot be zero.");
            });

            this.EnqueueTestComplete();
        }
    public async Task CreateListRemove_Crud_Validate()
    {
        using (var repository = new SqlYRepository(_connectionString)) {
            var newY = new EntityY {
                Name  = "Test",
                Price = 1
            };
            await repository.CreateAsync(newY);

            Assert.IsTrue(newY.Id > 0);
            //read and set lock on this record
            var readX = await repository.ReadAsync(newY.Id, "user");

            Assert.AreEqual(newY.Name, readX.Name);
            Assert.AreEqual(newY.Id, readX.Id);
            Assert.AreEqual(newY.Price, readX.Price);
            //delete record with lock
            await repository.DeleteAsync(newY.Id, "user");
        }
    }
Esempio n. 6
0
        public async Task <ActionResult> Edit(EntityY model)
        {
            try {
                using (var repository = new SqlYRepository()) {
                    try {
                        model.LockedBy = User.Identity.GetUserName();
                        await repository.UpdateAsync(model).ConfigureAwait(false);

                        TempData["Message"] = $"{model.Name} updated successfully.";
                    } catch (ConcurrencyException ex) {
                        TempData["Message"] = $"Record {model.Id} is locked by another user";
                    } 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"));
            }
        }
Esempio n. 7
0
        public void PersistAndFind()
        {
            {
                var ec1 = new EntityC();
                TestBase.TestSerialization(ec1);

                var ey  = new EntityY();
                var ec2 = new EntityC {
                    X = new[] { new EntityX(), new EntityX() }, Y = new[] { ey, ey }, Y2 = ey
                };
                TestBase.TestSerialization(ec2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ec1);
                    Assert.IsFalse(string.IsNullOrEmpty(ec1.Id));

                    em.Persist(ec2);
                    Assert.IsFalse(string.IsNullOrEmpty(ec2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ec1 = em.Find <EntityC>(ec1.Id);
                    Assert.AreEqual(ec1, _ec1);

                    var _ec2 = em.Find <EntityC>(ec2.Id);
                    Assert.AreEqual(ec2, _ec2);
                    Assert.AreSame(_ec2.Y[0], _ec2.Y[1]);
                    Assert.AreSame(_ec2.Y2, _ec2.Y[0]);
                }
            }

            {
                var ed1 = new EntityD();
                TestBase.TestSerialization(ed1);

                var ey  = new EntityY();
                var ed2 = new EntityD {
                    X = new HashSet <EntityX>(new[] { new EntityX(), new EntityX() }), Y = new HashSet <EntityY>(new[] { ey, new EntityY() }), Y2 = ey
                };
                TestBase.TestSerialization(ed2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ed1);
                    Assert.IsFalse(string.IsNullOrEmpty(ed1.Id));

                    em.Persist(ed2);
                    Assert.IsFalse(string.IsNullOrEmpty(ed2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ed1 = em.Find <EntityD>(ed1.Id);
                    Assert.AreEqual(ed1, _ed1);

                    var _ed2 = em.Find <EntityD>(ed2.Id);
                    Assert.AreEqual(ed2, _ed2);
                    Assert.IsNotNull(_ed2.Y.FirstOrDefault(item => object.ReferenceEquals(item, _ed2.Y2)));
                }
            }

            {
                var ee1 = new EntityE();
                TestBase.TestSerialization(ee1);

                var ey  = new EntityY();
                var ee2 = new EntityE {
                    X = new List <EntityX>(new[] { new EntityX(), new EntityX() }), Y = new List <EntityY>(new[] { ey, ey }), Y2 = ey
                };
                TestBase.TestSerialization(ee2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ee1);
                    Assert.IsFalse(string.IsNullOrEmpty(ee1.Id));

                    em.Persist(ee2);
                    Assert.IsFalse(string.IsNullOrEmpty(ee2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ee1 = em.Find <EntityE>(ee1.Id);
                    Assert.AreEqual(ee1, _ee1);

                    var _ee2 = em.Find <EntityE>(ee2.Id);
                    Assert.AreEqual(ee2, _ee2);
                    Assert.AreSame(_ee2.Y[0], _ee2.Y[1]);
                    Assert.AreSame(_ee2.Y2, _ee2.Y[0]);
                }
            }

            {
                var ef1 = new EntityF();
                TestBase.TestSerialization(ef1);

                var ey  = new EntityY();
                var ef2 = new EntityF {
                    X = new ArrayList(new[] { new EntityX(), new EntityX() }), Y = new ArrayList(new[] { ey, ey }), Y2 = ey
                };
                TestBase.TestSerialization(ef2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ef1);
                    Assert.IsFalse(string.IsNullOrEmpty(ef1.Id));

                    em.Persist(ef2);
                    Assert.IsFalse(string.IsNullOrEmpty(ef2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ef1 = em.Find <EntityF>(ef1.Id);
                    Assert.AreEqual(ef1, _ef1);

                    var _ef2 = em.Find <EntityF>(ef2.Id);
                    Assert.AreEqual(ef2, _ef2);
                    Assert.AreSame(_ef2.Y[0], _ef2.Y[1]);
                    Assert.AreSame(_ef2.Y2, _ef2.Y[0]);
                }
            }
        }
Esempio n. 8
0
        public void PersistAndFind()
        {
            {
                var ec1 = new EntityC();
                TestBase.TestSerialization(ec1);

                var ey = new EntityY();
                var ec2 = new EntityC { X = new[] { new EntityX(), new EntityX() }, Y = new[] { ey, ey }, Y2 = ey };
                TestBase.TestSerialization(ec2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ec1);
                    Assert.IsFalse(string.IsNullOrEmpty(ec1.Id));

                    em.Persist(ec2);
                    Assert.IsFalse(string.IsNullOrEmpty(ec2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ec1 = em.Find<EntityC>(ec1.Id);
                    Assert.AreEqual(ec1, _ec1);

                    var _ec2 = em.Find<EntityC>(ec2.Id);
                    Assert.AreEqual(ec2, _ec2);
                    Assert.AreSame(_ec2.Y[0], _ec2.Y[1]);
                    Assert.AreSame(_ec2.Y2, _ec2.Y[0]);
                }
            }

            {
                var ed1 = new EntityD();
                TestBase.TestSerialization(ed1);

                var ey = new EntityY();
                var ed2 = new EntityD { X = new HashSet<EntityX>(new[] { new EntityX(), new EntityX() }), Y = new HashSet<EntityY>(new[] { ey, new EntityY() }), Y2 = ey };
                TestBase.TestSerialization(ed2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ed1);
                    Assert.IsFalse(string.IsNullOrEmpty(ed1.Id));

                    em.Persist(ed2);
                    Assert.IsFalse(string.IsNullOrEmpty(ed2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ed1 = em.Find<EntityD>(ed1.Id);
                    Assert.AreEqual(ed1, _ed1);

                    var _ed2 = em.Find<EntityD>(ed2.Id);
                    Assert.AreEqual(ed2, _ed2);
                    Assert.IsNotNull(_ed2.Y.FirstOrDefault(item => object.ReferenceEquals(item, _ed2.Y2)));
                }
            }

            {
                var ee1 = new EntityE();
                TestBase.TestSerialization(ee1);

                var ey = new EntityY();
                var ee2 = new EntityE { X = new List<EntityX>(new[] { new EntityX(), new EntityX() }), Y = new List<EntityY>(new[] { ey, ey }), Y2 = ey };
                TestBase.TestSerialization(ee2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ee1);
                    Assert.IsFalse(string.IsNullOrEmpty(ee1.Id));

                    em.Persist(ee2);
                    Assert.IsFalse(string.IsNullOrEmpty(ee2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ee1 = em.Find<EntityE>(ee1.Id);
                    Assert.AreEqual(ee1, _ee1);

                    var _ee2 = em.Find<EntityE>(ee2.Id);
                    Assert.AreEqual(ee2, _ee2);
                    Assert.AreSame(_ee2.Y[0], _ee2.Y[1]);
                    Assert.AreSame(_ee2.Y2, _ee2.Y[0]);
                }
            }

            {
                var ef1 = new EntityF();
                TestBase.TestSerialization(ef1);

                var ey = new EntityY();
                var ef2 = new EntityF { X = new ArrayList(new[] { new EntityX(), new EntityX() }), Y = new ArrayList(new[] { ey, ey }), Y2 = ey };
                TestBase.TestSerialization(ef2);

                using (var em = Emf.CreateEntityManager())
                {
                    em.Persist(ef1);
                    Assert.IsFalse(string.IsNullOrEmpty(ef1.Id));

                    em.Persist(ef2);
                    Assert.IsFalse(string.IsNullOrEmpty(ef2.Id));
                }

                using (var em = Emf.CreateEntityManager())
                {
                    var _ef1 = em.Find<EntityF>(ef1.Id);
                    Assert.AreEqual(ef1, _ef1);

                    var _ef2 = em.Find<EntityF>(ef2.Id);
                    Assert.AreEqual(ef2, _ef2);
                    Assert.AreSame(_ef2.Y[0], _ef2.Y[1]);
                    Assert.AreSame(_ef2.Y2, _ef2.Y[0]);
                }
            }
        }