コード例 #1
0
 private void ModifyOpti(Guid id)
 {
     using (var cx = LastWins.CreateContext())
     {
         var opti = cx.Optis.Find(id);
         opti.Value++;
         opti.Updated    = DateTime.Now;
         opti.RowVersion = Guid.NewGuid();
         cx.SaveChanges();
     }
 }
 private void Delete(Guid id)
 {
     Task.Run(() =>
     {
         using (var cx = LastWins.CreateContext())
         {
             var lw = cx.LastWins.FirstOrDefault(e => e.Id == id);
             cx.LastWins.Remove(lw);
             cx.SaveChanges();
         }
     }).Wait();
 }
コード例 #3
0
 public void AddOpti()
 {
     using (var cx = LastWins.CreateContext())
     {
         var opti = new OptiEntity();
         opti.Id         = Guid.NewGuid();
         opti.Value      = 0;
         opti.RowVersion = Guid.NewGuid();
         opti.Created    = opti.Updated = DateTime.Now;
         cx.Optis.Add(opti);
         cx.SaveChanges();
     }
 }
        public void IgnoreEntityDeleted2()
        {
            var id = Guid.NewGuid();

            using (var cx = LastWins.CreateContext())
            {
                var lw = new LastWinsEntity();
                lw.Id      = id;
                lw.Name    = "v1";
                lw.Created = lw.Updated = DateTime.Now;
                cx.LastWins.Add(lw);
                cx.SaveChanges(); // save first one

                Delete(id);

                cx.LastWins.Remove(lw); // already deleted
                cx.SaveChanges(SaveChangesMode.IgnoreEntityDeleted);
            }
        }
        public void IgnoreEntityDeletedAndIgnoreEntityDublicateKeyError()
        {
            var id  = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            using (var cx = LastWins.CreateContext())
            {
                var lw2 = new LastWinsEntity();
                lw2.Id      = id2;
                lw2.Name    = "v1";
                lw2.Created = lw2.Updated = DateTime.Now;
                cx.LastWins.Add(lw2);
                cx.SaveChanges();
            }

            using (var cx = LastWins.CreateContext())
            {
                var lw = new LastWinsEntity();
                lw.Id      = id;
                lw.Name    = "v1";
                lw.Created = lw.Updated = DateTime.Now;
                cx.LastWins.Add(lw);
                cx.SaveChanges();

                Delete(id);        // meantime delete lw1

                lw.Name    = "v2"; // modify lw1
                lw.Updated = DateTime.Now;

                var lw3 = new LastWinsEntity(); // lw3 dublicate of lw2
                lw3.Id      = id2;
                lw3.Name    = "v1";
                lw3.Created = lw3.Updated = DateTime.Now;
                cx.LastWins.Add(lw3);

                cx.SaveChanges(SaveChangesMode.All);
            }
        }
        public void IgnoreEntityDublicateKeyError()
        {
            var id = Guid.NewGuid();

            using (var cx = LastWins.CreateContext())
            {
                var lw = new LastWinsEntity();
                lw.Id      = id;
                lw.Name    = "v1";
                lw.Created = lw.Updated = DateTime.Now;
                cx.LastWins.Add(lw);
                cx.SaveChanges(); // save first one
            }

            using (var cx = LastWins.CreateContext())
            {
                var lw2 = new LastWinsEntity();
                lw2.Id      = id;
                lw2.Name    = "v1";
                lw2.Created = lw2.Updated = DateTime.Now;
                cx.LastWins.Add(lw2);
                cx.SaveChanges(SaveChangesMode.IgnoreEntityDublicateKey);
            }
        }
コード例 #7
0
        public void AddOptiWithUpdateConflictSolving()
        {
            var id = Guid.NewGuid();
            var initialRowVersion = Guid.NewGuid();
            var lastRowVersion    = Guid.NewGuid();
            var oc = CreateOptiContext();

            var updateCounter = 0;

            oc.SelectFunc = (cx) =>
            {
                var i = cx.Optis.Find(id);
                return(i);
            };

            oc.AddAction = (cx) =>
            {
                var oe = new OptiEntity();
                oe.Id         = id;
                oe.Value      = 0;
                oe.Created    = oe.Updated = DateTime.Now;
                oe.RowVersion = initialRowVersion;
                cx.Optis.Add(oe);
                cx.SaveChanges();
            };
            oc.UpdateAction = (cx, oe) =>
            {
                // test information
                updateCounter++;
                if (updateCounter == 2)
                {
                    Assert.AreNotEqual(initialRowVersion, oe.RowVersion);
                }

                // update the entity
                lastRowVersion = Guid.NewGuid();
                oe.Value++;
                oe.Created    = oe.Updated = DateTime.Now;
                oe.RowVersion = lastRowVersion;
                cx.SaveChanges();
            };

            // initial add opti entity
            oc.Execute();

            // simulate Execute part 1 -> create context and select entity
            var cx2    = oc.ContextFactory();
            var entity = oc.SelectFunc(cx2);

            // modify in the meantime in another dbcontext
            ModifyOpti(id);

            // simulate Execute part 2 -> TryUpdate the existing entity. Its  here, but modified
            oc.TryUpdate(cx2, entity);

            // update must be called twice!
            Assert.AreEqual(2, updateCounter);

            // check if the last row version is stored in the database
            using (var cx = LastWins.CreateContext())
            {
                var opti = cx.Optis.Find(id);
                Assert.AreEqual(lastRowVersion, opti.RowVersion);
            }
        }
コード例 #8
0
        public OptiContext <SqlDbContext, OptiEntity> CreateOptiContext()
        {
            var result = new OptiContext <SqlDbContext, OptiEntity>(() => LastWins.CreateContext());

            return(result);
        }