Esempio n. 1
0
        /// <summary>
        /// This method must be called after the connection string and database name have been set, but before
        /// any other operations are called.  The best place is at the end of the constructor of the repository's
        /// implementation, or in the constructor of a derived abstract base class.
        /// </summary>
        protected void ConnectToDatabase()
        {
            if (Db.Initialized)
            {
                return;
            }

            if (string.IsNullOrEmpty(ConnectionString))
            {
                throw new Exception("A connection string has not been specified.  Have you overridden BaseMongoRepository?");
            }

            if (string.IsNullOrEmpty(DatabaseName))
            {
                throw new Exception("A database name has not been specified.  Have you overridden BaseMongoRepository?");
            }

            try
            {
                Db.Connect(ConnectionString, DatabaseName);
            }
            catch (Exception)
            {
                throw new Exception("Could not connect to the mongo database");
            }
        }
Esempio n. 2
0
        public MongoRepository(IMongoDbContext dbContext)
        {
            _db = dbContext;

            // initialize MongoDB
            if (!_db.Initialized)
            {
                try
                {
                    _db.Connect("mongodb://127.0.0.1/?safe=true", "sqrlserverexample");
                }
                catch (Exception)
                {
                    Console.Error.WriteLine("Could not connect to the database");
                }
            }
        }