I'm using Mongo Repository to talk to a mongo database.
Here is my connection string.
<connectionStrings>
<!-- See http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Connectionstrings for more info -->
<add name="MongoServerSettings" connectionString="mongodb://myDomain:27017/myDatabase1" />
</connectionStrings>
I then use it like this:
using System;
using System.Linq;
using MongoRepository;
class Program
{
static MongoRepository<Customer> customerrepo = new MongoRepository<Customer>();
static void Main(string[] args)
{
//Add customers
var john = new Customer() { FirstName = "John", LastName = "Doe" };
var jane = new Customer() { FirstName = "Jane", LastName = "Doe" };
var jerry = new Customer() { FirstName = "Jerry", LastName = "Maguire" };
customerrepo.Add(new[] { john, jane, jerry });
}
}
This works fine. But how can I configure it so it can talk to 2 or more databases.
Thanks.
The MongoRepository<T>
class has a constructor that takes a connection string or a mongo url as parameter.
If you want a repository on myDatabase2 just instante a MongoRepository with the correct connection string. Ex:
var customerRepoOnDb2 = new MongoRepository<Customer>("mongodb://myServer/myDatabase2");
You can add the connection string to your config:
<connectionStrings>
<add name="MongoServerSettings" connectionString="mongodb://myDomain:27017/myDatabase1" />
<add name="MongoServerSettings2" connectionString="mongodb://..." />
</connectionStrings>
And:
var customerRepoOnDb2 = new MongoRepository<Customer>(ConfigurationManager.ConnectionStrings["MongoServerSettings2"].ConnectionString);