Multiple contexts to different databases

Sep 19, 2014 at 8:41 AM
Great job on this tool. I can see that a lot of work has gone into this and its potential a huge benefit in testing mappings (I have a really complex object model).

I would imagine my situation is somewhat unique and for that reason I'm not surprised this isn't working for me but I thought I would describe what I'm seeing in case you have some ideas on what I might try to do. I have a > EF6 code first migrations solution that uses multiple contexts to different databases.

In my unit test I will have two contexts; One for the admin database which gives me the tenant connection string and another for the actual tenant database itself. I've got a tenant-per-database approach which works fine outside of NUnit. At startup, the application loads all the tenant connection details from the Admin db into a collection so that a factory can create the right custom dbcontext class for the right tenant db based on the URL.

So when I run my unit test I can see the Admin Database is initialised correctly with the schema.Tables collection populated however subsequently, the Tenant database is not initialised. I can see that in the GetDbSchema method in DbSchemaStore the metadata doesn't contain my tenant db tables so an exception occurs in GetTable later on.

Is there something inherent in Effort that prevents two mocked dbcontexts from existing?
When I look at the concurrentcache "store" collection in DbSchemaStore I see 2 items. One has only the _MigrationHistory defined and the other has my admin database tables defined. I presume that first one is my tenant db?
I have tried both CreatePersistant and CreateTransient DbConnections with the former having the instanceid being set in which case I do see this in the connection I am creating being used correctly throughout the call stack. The EffortProviderServices.DbCreateDatabase method has a storeItemCollection parameter which contains a last element of CodeFirstDatabase and that has no EntitySets. That method is called by EF so I'm not even sure this is a Effort based problem. No pun intended :)

What could be happening here?
Sep 19, 2014 at 9:10 AM
This was indeed an EF configuration issue. The admin db used a DbInitializer that derived from CreateDatabaseIfNotExists whereas the tenant db initializer derived from MigrateDatabaseToLatestVersion. I had to add the following in for my testing code path....
mycustomcontext.Database.CreateIfNotExists();
Marked as answer by MattGC on 9/19/2014 at 2:17 AM
Coordinator
Sep 23, 2014 at 3:41 PM
It is pleased to see that you have managed to solve the issue on your own. Nice job!

I have been always concerned about the relation of Effort and Code First. Recently I improved the message of the exception coming from GetTable method. Wonder if it would have helped you out.