Пример #1
0
        public static MongoDatabase InitializeTestDatabase(bool dropExistingDatabase = true)
        {
            var connectionStringSettings = ConfigurationManager.ConnectionStrings["mongotestdb"];
            if (connectionStringSettings == null)
            {
                throw new ConfigurationErrorsException("Could not find MongoDB test database connection string with the name 'mongotestdb' in app.config");
            }
            
            var url = new MongoUrl(connectionStringSettings.ConnectionString);
            var databaseName = GetDatabaseName(url);
            var database = new MongoClient(url).GetServer().GetDatabase(databaseName);

            Console.WriteLine("Using Mongo database '{0}'", databaseName);
            if (dropExistingDatabase)
            {
                Console.WriteLine("Dropping Mongo database '{0}'", databaseName);
                database.Drop();
            }

            return database;
        }