示例#1
0
        //--------------------------------------------------------------
        //private methods

        private int RunSqlTransactionWithNoSqlWrite(DbContext sqlContext, Func <int> callBaseSaveChanges)
        {
            if (sqlContext.Database.CurrentTransaction != null)
            {
                throw new InvalidOperationException("You can't use the NoSqlBookUpdater if you are using transactions.");
            }

            var applier = new ApplyChangeToNoSql(sqlContext, _noSqlContext);

            using (var transaction = sqlContext.Database.BeginTransaction())
            {
                var result = callBaseSaveChanges(); //Save the SQL changes
                applier.UpdateNoSql(_bookChanges);  //apply the book changes to the NoSql database
                _noSqlContext.SaveChanges();        //And Save to NoSql database
                transaction.Commit();
                return(result);
            }
        }
        public TestNoSqlBookRead(ITestOutputHelper output)
        {
            _output = output;
            var config  = AppSettings.GetConfiguration();
            var builder = new DbContextOptionsBuilder <NoSqlDbContext>()
                          .UseCosmos(
                config["endpoint"],
                config["authKey"],
                GetType().Name);

            _options = builder.Options;

            using var context = new NoSqlDbContext(_options);
            context.Database.EnsureCreated();
            if (context.Books.Select(_ => 1).AsEnumerable().Count() < 5)
            {
                var books = NoSqlTestData.CreateDummyBooks(10, true);
                context.AddRange(books);
                context.SaveChanges();
            }
        }