Пример #1
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());
            }
        }
Пример #2
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);
            }
        }
Пример #3
0
        public async Task <BatchResult> Handle(GetServerAddresses request, CancellationToken cancelToken)
        {
            var game = await GameContext.FindGameOrThrowAsync(request.Info).ConfigureAwait(false);

            var sGame = game as IQueryServers;

            if (sGame == null)
            {
                throw new ValidationException("Game does not support servers");
            }
            var fetched   = false;
            var scope     = _scopeLoc.Scope;
            var addresses = await
                            DbContextLocator.GetApiContext()
                            .GetOrAddServers(game.Id, async() => {
                fetched  = true;
                var list = new List <IPEndPoint>();
                await sGame.GetServers(_sqf, cancelToken, x => {
                    list.AddRange(x);
                    scope.SendMessage(new ServersPageReceived(game.Id, x));
                }).ConfigureAwait(false);
                return(list);
            })
                            .ConfigureAwait(false);

            if (!fetched)
            {
                foreach (var b in addresses.Batch(231))
                {
                    cancelToken.ThrowIfCancellationRequested();
                    scope.SendMessage(new ServersPageReceived(game.Id, b.ToList()));
                }
            }
            return(new BatchResult(addresses.Count));
        }
Пример #4
0
 public async Task Handle(RemoveExtension request)
 {
     var ctx = DbContextLocator.GetSettingsContext();
     await
     _installer.Uninstall(_destination, await ctx.GetSettings().ConfigureAwait(false), _filePaths)
     .ConfigureAwait(false);
 }
Пример #5
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();
            }
        }
Пример #6
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);
            }
        }
Пример #7
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();
                }
            }
        }