EF5 Code First with Multiple Databases

This turned out to be fairly simple, but it took me a while to figure out what was going on. I have two databases configured for one application. One is an authentication, authorization, and loggging datbase – it makes use of the default security framework with ASP.NET MVC 5 projects. In addition it utilizes NLog for logging. The second database is for application data.

So essentially I have two connection strings:


I have the default IdentityDbContext (included by default as part of the IdentityModels.cs file ) from an “out-of-the-box” MVC 5 application (with individual user accounts). To this, I added a new class “Log”, and included it in the context by simply adding a new DBSet to the properties of the context object.

In addition I added a new DbContext subclass, AppDataContext, with my two initial objects (very early in this project).

So my goal here was to simply be able to keep both contexts alive, and use package manager to add-migration(s) and update-database(s) for each connection string. However, out of the box this immediately runs into issues. Once you enable migrations for one, you must rename it before you can enable migrations for the other, because it wants to create the file with the default template, and you already have a configuration.cs file. To the credit of the product, they don’t let you do this without warning. However, that is not the desired result for me. I need to have both alive and well.

So I simply renamed Configuration.cs to IdentityConfiguration.cs, then enabled migrations again specifying the AppData context. And I got my second file. For the sake of consistency I renamed it as well, to AppDataConfiguration.cs. However, this is not sufficient. Because now, when trying to add migrations, the first one adds fine. The second, however, insists that you apply the first before you can continue.

Of course this made sense, since with EF Code First you have to apply a migration before you can add another. So I simply popped into the package manager and ran update-database providing the -configurationTypeName pointing to my IdentityConfiguration.cs file. This worked swimmingly, so I went back to add my migration for the AppDataConfiguration, when I was told that it was necessary to apply a pending migration. Since I’d just applied my only pending migration, this didn’t sit well with me. So I turned to the internet and found a thread on Stack Overflow (a site I highly recommend), which helped me resolve the root issue.

Even though I’d changed the names of both files, and classes, I had kept them both in the same namespace. Apparently, for EF CF Migrations, this is not cool. So I changed the namespaces and class names (to make it easier) from





This was the trick, it all works fine, and as I’d expected, once you put everything into the distinct namespaces. While it is an annoying issue, at least it’s a very simple work around. By applying this fix you can have many database connections managed by EFCF.

Leave a Reply

Your email address will not be published. Required fields are marked *