MongoContext传递模型对象和集合名称

时间:2016-07-24 15:02:54

标签: c# mongodb asp.net-web-api2

我当前的工作代码`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();

我想有可能通过collectionNamemodel object更灵活

  public IMongoCollection<model object> Objs => Database.GetCollection<model object>(collectionName);

0 个答案:

没有答案