/// <summary> /// 从指定的连接字符串创建并返回MongoDatabase。 /// </summary> /// <param name="connectionString">用于从中获取数据库的connectionstring。</param> /// <returns>从指定的connectionstring返回MongoDatabase。</returns> public static MongoDatabase GetDatabaseFromConnectionString(string connectionString) { var conMongoUrl = new MongoUrl(connectionString); var server = MongoServer.Create(conMongoUrl.ToServerSettings());//旧的写法:创建MongoDB连接,并获取MongoDB服务 return(server.GetDatabase(conMongoUrl.DatabaseName)); }
/** * 初始化连接池,设置参数。 */ private static void init() { var connectString = ConfigurationManager.ConnectionStrings["MongoServerSettings"].ConnectionString; MongoUrl cnn = new MongoUrl(connectString); mongo = new MongoServer(cnn.ToServerSettings()); dbName = cnn.DatabaseName; }
public static IContainer Initialize() { ObjectFactory.Initialize(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); x.For <IFilterProvider>().Use <SmFilterProvider>(); x.For <IFlexMembershipProvider>().HybridHttpOrThreadLocalScoped().Use <FlexMembershipProvider>(); x.For <IFlexRoleProvider>().HybridHttpOrThreadLocalScoped().Use <FlexRoleProvider>(); x.For <IFlexUserStore>().HybridHttpOrThreadLocalScoped().Use <FlexMembershipUserStore <User, Role> >(); x.For <IFlexRoleStore>().HybridHttpOrThreadLocalScoped().Use <FlexMembershipUserStore <User, Role> >(); x.SetAllProperties(p => p.OfType <IFlexRoleProvider>()); x.Forward <IFlexMembershipProvider, IFlexOAuthProvider>(); x.For <IApplicationEnvironment>().Singleton().Use <AspnetEnvironment>(); x.For <ISecurityEncoder>().Singleton().Use <DefaultSecurityEncoder>(); x.For <MongoDatabase>().Singleton().Use(c => { var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionString"].ConnectionString; if (String.IsNullOrEmpty(connectionString)) { throw new ConfigurationErrorsException("MongoConnectionString was not found in the App/Web.config."); } var url = new MongoUrl(connectionString); var server = MongoServer.Create(url.ToServerSettings()); var database = server.GetDatabase(url.DatabaseName); BsonClassMap.RegisterClassMap <User>(ctx => { ctx.SetIgnoreExtraElements(true); ctx.AutoMap(); ctx.SetIdMember(ctx.GetMemberMap(u => u.Id).SetRepresentation(BsonType.ObjectId)); }); BsonClassMap.RegisterClassMap <Role>(ctx => { ctx.SetIgnoreExtraElements(true); ctx.AutoMap(); ctx.SetIdMember(ctx.GetMemberMap(r => r.Id).SetRepresentation(BsonType.ObjectId)); }); var userCollection = database.GetCollection <User>("user"); var roleCollection = database.GetCollection <User>("role"); userCollection.EnsureIndex(IndexKeys <User> .Ascending(u => u.Username), IndexOptions.SetUnique(true)); userCollection.EnsureIndex(IndexKeys.Ascending("OAuthAccounts.Provider", "OAuthAccounts.ProviderUserId"), IndexOptions.SetUnique(true)); roleCollection.EnsureIndex(IndexKeys <Role> .Ascending(r => r.Name), IndexOptions.SetUnique(true)); roleCollection.EnsureIndex(IndexKeys <Role> .Ascending(r => r.Users)); return(database); }); x.For <MongoCollection <User> >().Use(c => c.GetInstance <MongoDatabase>().GetCollection <User>("user")); x.For <MongoCollection <Role> >().Use(c => c.GetInstance <MongoDatabase>().GetCollection <Role>("role")); }); return(ObjectFactory.Container); }