Пример #1
0
        public void Saves_Successfully()
        {
            using (
                var dbContextScope =
                    _dbContextScopeFactory.CreateAmbientDbContextInTransactionMode <BloggerDbContext>())
            {
                //Get the current DbContext of type BloggerDbContext
                var context = DbContextLocator.GetDbContext <BloggerDbContext>();
                //Adding Blog to the database.
                var blog = new Blog
                {
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now,
                    BlogUser    = new User
                    {
                        Name       = "TestUser",
                        Occupation = "Software Developer",
                    },
                    Overview = "This is a test overview"
                };
                context.Blogs.Add(blog);
                var post = new Post
                {
                    Content          = "Test Content",
                    Meta             = "Test",
                    ShortDescription = "This is an example test content",
                    Title            = "Ambient Simple Test Context"
                };
                blog.BlogPost = post;
                dbContextScope.SaveAndCommitChanges();

                Assert.That(context.Blogs.Count() == 1);
                Assert.That(context.Posts.Count() == 1);
            }
        }
Пример #2
0
        private async Task AddBlog()
        {
            using (
                var dbContextScope2 =
                    _dbContextScopeFactory.CreateAmbientDbContextInTransactionMode <BloggerDbContext>())
            {
                //Get the current DbContext of type BloggerDbContext
                var context2 = DbContextLocator.GetDbContext <BloggerDbContext>();

                //Adding Blog to the database.
                var blog2 = new Blog
                {
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now,
                    BlogUser    = new User
                    {
                        Name       = "TestUser",
                        Occupation = "Software Developer",
                    },
                    Overview = "This is a test overview"
                };
                context2.Blogs.Add(blog2);

                var post = new Post
                {
                    Content          = "Test Content",
                    Meta             = "Test",
                    ShortDescription = "This is an example test content",
                    Title            = "Ambient Simple Test Context"
                };
                blog2.BlogPost = post;
                await dbContextScope2.SaveAndCommitChangesAsync(new CancellationToken());
            }
        }
Пример #3
0
        public void Saves_Successfully_WhenExternalTransactionUsed()
        {
            var initializer = new CreateDatabaseIfNotExists <BloggerDbContext>();

            Database.SetInitializer(initializer);
            initializer.InitializeDatabase(new BloggerDbContext());
            using (var conn = new SqlConnection(@"Server=.\SqlExpress;Database=Blog;Integrated Security=true;Trusted_Connection=true"))
            {
                conn.Open();

                using (var sqlTxn = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    using (
                        var dbContextScope2 =
                            _dbContextScopeFactory.CreateAmbientDbContextWithExternalTransaction <BloggerDbContext>(sqlTxn, conn))
                    {
                        //Get the current DbContext of type BloggerDbContext
                        var context2 = DbContextLocator.GetDbContext <BloggerDbContext>();

                        //Adding Blog to the database.
                        var blog2 = new Blog
                        {
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            BlogUser    = new User
                            {
                                Name       = "TestUser",
                                Occupation = "Software Developer",
                            },
                            Overview = "This is a test overview"
                        };
                        context2.Blogs.Add(blog2);

                        var post = new Post
                        {
                            Content          = "Test Content",
                            Meta             = "Test",
                            ShortDescription = "This is an example test content",
                            Title            = "Ambient Simple Test Context"
                        };
                        blog2.BlogPost = post;
                        dbContextScope2.SaveAndCommitChanges();
                        Assert.That(context2.Blogs.Count() == 1);
                    }
                    sqlTxn.Commit();
                }
                conn.Close();
                conn.Dispose();
            }
        }
Пример #4
0
        public void Should_SaveSuccessfully_WhenUsingNestedAsyncMethods()
        {
            using (
                var dbContextScope2 =
                    _dbContextScopeFactory.CreateAmbientDbContextInTransactionMode <BloggerDbContext>())
            {
                //Get the current DbContext of type BloggerDbContext
                var context2 = DbContextLocator.GetDbContext <BloggerDbContext>();

                var task = new Task(async() => await AddBlog());
                task.Start();
                task.Wait();
                Assert.That(!context2.Blogs.Any());
                Assert.That(!context2.Posts.Any());
                dbContextScope2.SaveAndCommitChanges();
                Assert.That(context2.Blogs.Count() == 1);
            }
        }
Пример #5
0
        public void Should_ThrowInvalidOperationException_WhenParentInRead_AndChildInWriteMode()
        {
            using (_dbContextScopeFactory.CreateAmbientDbContextInReadonlyMode <BloggerDbContext>())
            {
                using (
                    var dbContextScope2 =
                        _dbContextScopeFactory.CreateAmbientDbContextInTransactionMode <BloggerDbContext>())
                {
                    //Get the current DbContext of type BloggerDbContext
                    var context = DbContextLocator.GetDbContext <BloggerDbContext>();
                    //Adding Blog to the database.
                    var blog = new Blog
                    {
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now,
                        BlogUser    = new User
                        {
                            Name       = "TestUser",
                            Occupation = "Software Developer",
                        },
                        Overview = "This is a test overview"
                    };
                    context.Blogs.Add(blog);

                    var post = new Post
                    {
                        Content          = "Test Content",
                        Meta             = "Test",
                        ShortDescription = "This is an example test content",
                        Title            = "Ambient Simple Test Context"
                    };
                    blog.BlogPost = post;
                    dbContextScope2.SaveAndCommitChanges();
                }
            }
        }