예제 #1
0
        public void IgnoreEntityDublicateKeyError()
        {
            var id = Guid.NewGuid();

            // create first entity with id (primary key)
            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
            }

            // create second entity with same id (primary key)
            // -> Dublicate key error will be ignored,
            // -> second entity will not be added to db
            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);
            }
        }
예제 #2
0
        public void NotIgnoreEntityDublicateKeyError()
        {
            var id = Guid.NewGuid();

            // create first entity with id
            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
            }

            // create second entity with same id -> will throw a DbUpdateException
            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();
            }
        }
 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();
     }
 }
예제 #4
0
 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();
 }
 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 AddOrUpdate()
        {
            var id = Guid.NewGuid();
            var initialRowVersion = Guid.NewGuid();
            var lastRowVersion    = Guid.NewGuid();

            // Create a new OptiContext.
            // - Pass a factory method to create a new DbContext
            var oc = new OptiContext <SqlDbContext, OptiEntity>(() => LastWins.CreateContext());

            // defines the method to select an entity
            oc.SelectFunc = (cx) =>
            {
                var i = cx.Optis.Find(id);
                return(i);
            };

            // defines the method to add an entity, if select returns no entity
            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();
            };

            // defines the method to update the entity, if select returns a entity
            oc.UpdateAction = (cx, oe) =>
            {
                // 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();
        }
예제 #7
0
        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);
            }
        }
예제 #8
0
        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 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);
            }
        }
        public OptiContext <SqlDbContext, OptiEntity> CreateOptiContext()
        {
            var result = new OptiContext <SqlDbContext, OptiEntity>(() => LastWins.CreateContext());

            return(result);
        }