public void Commit_DataShouldBeAvailableInSeparatedContextsAfterCommit()
        {
            var dbFactory  = CreateDbFactory("Commit_DataShouldBeAvailableInSeparatedContextsAfterCommit");
            var uowFactory = new EntityFrameworkUnitOfWorkFactory <BloggingContext>(dbFactory, new UnitOfWorkRegistry());

            using (var uow = uowFactory.Create())
            {
                Assert.AreEqual(0, uow.Context.Blog.Count());

                CreateRecord(uowFactory);
                //Data are not stored until commit of this UOW
                Assert.AreEqual(1, uow.Context.Blog.Count());
                var context = dbFactory.Create();//Do not dispose here the database, otherwise it will be deleted

                //Data are not available also from another separated context
                Assert.AreEqual(0, context.Blog.Count());

                //Store changes
                uow.Commit();

                //Data are also into separated context
                Assert.AreEqual(1, context.Blog.Count());

                //Changes are in UoW context
                Assert.AreEqual(1, uow.Context.Blog.Count());
            }
        }
 private void CreateRecord(EntityFrameworkUnitOfWorkFactory <BloggingContext> uowFactory)
 {
     using (var uow = uowFactory.Create())
     {
         uow.Context.Blog.Add(new Blog
         {
             Name = "MyName"
         });
         uow.Commit();
     }
 }
        public void ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater()
        {
            using (var dbFactory = new BloggingContextFactory("ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater"))
            {
                var    uowFactory      = new EntityFrameworkUnitOfWorkFactory <BloggingContext>(dbFactory, new UnitOfWorkRegistry());
                var    repository      = new BlogRepository(uowFactory, dbFactory);
                string defaultBlogName = "Default name";
                string newBlogName     = "UpdatedBlogName";

                //Create record
                using (var uow = uowFactory.Create())
                {
                    var context = uow.Context;
                    Assert.AreEqual(0, context.Blog.Count());
                    context.Blog.Add(new Blog(defaultBlogName));
                    uow.Commit();
                    Assert.AreEqual(1, context.Blog.Count());
                }
                //Read outside from UoW
                var blog = repository.GetByName(defaultBlogName);
                Assert.IsNotNull(blog);

                //Update by another UoW
                using (var uow = uowFactory.Create())
                {
                    blog.Name = newBlogName;
                    repository.Update(blog);
                    uow.Commit();
                }

                //Read from outside UoW and get updated value

                var updatedBlog = repository.GetById(blog.Id);
                Assert.IsNotNull(updatedBlog);
                Assert.AreEqual(newBlogName, updatedBlog.Name);
            }
        }
        public void InsertEntityWithKeyToBeNotEmptyInteger()
        {
            using (var dbFactory = new BloggingContextFactory("ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater"))
            {
                var uowFactory = new EntityFrameworkUnitOfWorkFactory <BloggingContext>(dbFactory, new UnitOfWorkRegistry());

                var repository = new BlogRepository(uowFactory, dbFactory);
                var blog       = new Blog("Default name")
                {
                    Id = 100
                };

                //Create record
                using (var uow = uowFactory.Create())
                {
                    var context = uow.Context;
                    Assert.AreEqual(0, context.Blog.Count());
                    repository.Insert(blog);
                    uow.Commit();
                    Assert.AreEqual(1, context.Blog.Count());
                    Assert.AreEqual(1, context.Blog.First().Id);
                }
            }
        }