/// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public MongoDatabaseConvention([NotNull] DbContext dbContext)
 {
     _dbContext = Check.NotNull(dbContext, nameof(dbContext));
     _mongoDbOptionsExtension = _dbContext
                                .GetService <IDbContextServices>()
                                .ContextOptions
                                .FindExtension <MongoDbOptionsExtension>();
 }
Exemple #2
0
        /// <summary>
        /// Sets the name of the MongoDB database to use with the <see cref="DbContext"/> being configured.
        /// </summary>
        /// <param name="databaseName">The name of the MongoDB database instance to use with the current <see cref="DbContext"/>.</param>
        /// <returns>This <see cref="MongoDbOptionsExtension"/>, so that calls can be chained.</returns>
        public MongoDbContextOptionsBuilder UseDatabase([NotNull] string databaseName)
        {
            Check.NotEmpty(databaseName, nameof(databaseName));
            MongoDbOptionsExtension extension = CloneExtension();

            extension.DatabaseName = databaseName;
            ((IDbContextOptionsBuilderInfrastructure)OptionsBuilder).AddOrUpdateExtension(extension);
            return(this);
        }
Exemple #3
0
        private static DbContextOptionsBuilder SetupMongoDb(
            [NotNull] DbContextOptionsBuilder optionsBuilder,
            [NotNull] Action <MongoDbOptionsExtension> mongoDbOptionsExtensionAction,
            [CanBeNull] Action <MongoDbContextOptionsBuilder> mongoDbOptionsAction)
        {
            MongoDbOptionsExtension extension = GetOrCreateExtension(optionsBuilder);

            mongoDbOptionsExtensionAction(extension);
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

            ConfigureWarnings(optionsBuilder);

            mongoDbOptionsAction?.Invoke(new MongoDbContextOptionsBuilder(optionsBuilder));

            return(optionsBuilder);
        }
        public MongoDbConnection([NotNull] IDbContextOptions options, [NotNull] ILogger <MongoDbConnection> logger)
        {
            Check.NotNull(options, nameof(options));
            Check.NotNull(logger, nameof(logger));

            _logger = logger;

            var mongoDbOptions = MongoDbOptionsExtension.Extract(options);

            if (!string.IsNullOrWhiteSpace(mongoDbOptions.ConnectionString) &&
                !string.IsNullOrWhiteSpace(mongoDbOptions.DatabaseName))
            {
                _connectionString = mongoDbOptions.ConnectionString;
                _databaseName     = mongoDbOptions.DatabaseName;
            }
            else
            {
                throw new InvalidOperationException("A MongoDB store has been configured without specifying the connection string to use.");
            }
        }