Targeting Multiple Databases

MicroLite supports targeting multiple databases within a single application by simply adding multiple named connections and then creating a SessionFactory for each connection.

This can be useful if you need to aggregate data from multiple databases within a single application, or you may have a separate database (or different database user) for read and write operations for your application.

class Program
{
    // Ideally these would be registered in an IOC container instead of being static properties.
    public static ISessionFactory NorthwindSessionFactory { get; private set; }
    public static ISessionFactory PubsSessionFactory { get; private set; }

    static void Main(string[] args)
    {
        Program.NorthwindSessionFactory = Configure
            .Fluently()
            .ForConnection("NorthwindDB")
            .CreateSessionFactory();

        Program.PubsSessionFactory = Configure
            .Fluently()
            .ForConnection("PubsDB")
            .CreateSessionFactory();
    }
}

If you are using the MVC Extension, you can simply specify a name in the MicroLiteSessionAttribute to configure which database to connect to:

[MicroLiteSession("NorthwindDB")]
public class CustomerController : MicroLiteController

This article was written for version 2.1.0 of the MicroLite framework.

Advertisements

2 thoughts on “Targeting Multiple Databases

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s