我当前的工作代码`MongoContext'
namespace MC.API
{
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using MC.API.Properties;
using MC.DataModel.Models;
public class MongoContext
{
public IMongoDatabase Database;
public GridFSBucket ImagesBucket { get; set; }
public MongoContext()
{
var connectionString = Settings.Default.MCConnectionString;
var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
settings.ClusterConfigurator = builder => builder.Subscribe(new Log4NetMongoEvents());
var client = new MongoClient(settings);
Database = client.GetDatabase(Settings.Default.MCDatabaseName);
ImagesBucket = new GridFSBucket(Database);
}
public IMongoCollection<Deal> Deals => Database.GetCollection<Deal>("deals");
}
}
我在控制器中创建实例
public MongoContext Context = new MongoContext();
我想有可能通过collectionName
和model object
更灵活
public IMongoCollection<model object> Objs => Database.GetCollection<model object>(collectionName);