public static IServiceCollection AddMongoDbContext <TContext>(this IServiceCollection services, [CanBeNull] Action <MongoDbContextOptions> optionAction) where TContext : MongoDbContextBase { MongoDbContextOptions options = new MongoDbContextOptions(); optionAction(options); services.AddSingleton <MongoDbContextOptions>(options); services.AddScoped <MongoDbContextBase, TContext>(); return(services); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service"></param> /// <param name="options"></param> /// <param name="builder"></param> public static void AddMongoDbContext <T>(this IServiceCollection service, MongoDbContextOptions <T> options, MongoDbBuilder <T> builder = null) where T : MongoContext { service.AddSingleton(x => options); service.AddScoped <T>(); var context = service.BuildServiceProvider().GetRequiredService <T>(); builder?.Build(context); }
public MongoDbContext(IOptions <MongoDbContextOptions> optionAccessor) { options = optionAccessor.Value; var client = new MongoClient(options.ConnectionString); if (client != null) { _database = client.GetDatabase(options.Database); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service"></param> /// <param name="setupAction"></param> /// <param name="builder"></param> public static IMongoDbContextBuilder <T> AddMongoDbContext <T>(this IServiceCollection service, Action <MongoDbContextOptions <T>, string> setupAction) where T : MongoContext { var options = new MongoDbContextOptions <T>(); setupAction(options, typeof(T).Name.TrimEnd("Context")); service.AddSingleton(x => options); service.AddScoped <T>(); return(new MongoDbContextBuilder <T>(service)); }
public MongoDbContext(ILazyServiceProvider serviceProvider, MongoDbContextOptions dbContextOptions) { _id = Guid.NewGuid().ToString(); _serviceProvider = serviceProvider; _dbContextOptions = dbContextOptions; ResolveConfig(); AddToUow(); if (!_dbContextOptions.LazyConnect) { CheckStateAndConnect(); } }
public void ContextCreatedWithOptions() { var options = new MongoDbContextOptions { ConnectionString = TestConfiguration.ConnectionString, Database = TestConfiguration.GetDatabaseName() }; using (var context = new MongoDbContextTestContext(options)) { context.ContextDbSet.Add(new MongoDbContextModel()); Assert.IsFalse(context.ContextDbSet.Any()); context.SaveChanges(); Assert.IsTrue(context.ContextDbSet.Any()); } }
/// <summary> /// 注册 MongoDB 服务 /// </summary> /// <param name="services"></param> /// <param name="optionsAction">配置参数</param> /// <returns></returns> public static IServiceCollection AddMongoDB(this IServiceCollection services, Action <MongoDbContextOptions> optionsAction) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (optionsAction == null) { throw new ArgumentNullException(nameof(optionsAction)); } var options = new MongoDbContextOptions(); optionsAction(options); services.AddSingleton(new MongoDbContext(options)); services.AddSingleton <IMongoDatabaseProvider, MongoDatabaseProvider>(); return(services); }
public BlincatMongoDbContext(ILogger logger, MongoDbContextOptions options) : base(options) { _logger = logger; }
public MongoDbContextBase([NotNull] MongoDbContextOptions options) { _options = options; }
public DefaultMongoDbContext([NotNull] MongoDbContextOptions options) : base(options) { }
public AppDbContext(MongoDbContextOptions options) : base(options) { }
public ExampleContext(MongoDbContextOptions <ExampleContext> options) : base(options) { }
public PagesDbContext(MongoDbContextOptions options) : base(options) { }
protected MongoDbContextBase([NotNull] MongoDbContextOptions options) { _options = options; }
public TestDbContext(MongoDbContextOptions options, TestService testService) : base(options) { this.testService = testService ?? throw new System.ArgumentNullException(nameof(testService)); }
public MongoContext(MongoDbContextOptions options) { this.options = options; }
public TestDbContext(MongoDbContextOptions options) : base(options) { }
public WorkerMongoDbContext(MongoDbContextOptions options) : base(options) { }