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();
        }
        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);
            }
        }