Search code examples
c#.netentity-framework

EF6 'DbConfigurationClass' was set but this type was not discovered - multiple DbContexts and DbConfigurations


I have a solution in which we have two DbContexts, and we are in the process of moving from EF4 to EF6. The older DbContext was a code-first and we are mostly using the newer generated db-first, but need both in operation due to external dependencies.

My classes look like this:

namespace Old.Busted.EF.DAL
{
    [DbConfigurationType(typeof(Old.Busted.EF.DAL.OldConfiguration))]
    public class OldContext : DbContext[...]

    public class OldConfiguration : DbConfiguration[...]
}

namespace New.Shiny.EF.DAL
{
    [DbConfigurationType(typeof(New.Shiny.EF.DAL.NewConfiguration))]
    public class NewContext : DbContext[...]

    public class NewConfiguration : DbConfiguration[...]
}

The precise error that I am getting is

An instance of 'NewConfiguration' was set but this type was not discovered in the same assembly as the 'OldContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file.

which is trying to apply the new configuration to the old context. The library in which the breaking code is sitting is the only library which references both the old and new EF DALs, and moreover this exception only gets thrown when the tests are being run on the command line via mstest - they pass just fine running from within Visual Studio.

Using .NET 4.0 and Visual Studio 2010.

Things I have tried:

  • putting config info in config files instead of code (no change)
  • putting a single DbConfiguration into a shared library (broke even more things)
  • using a DbContext constructor passing in a DbConnection object instead of the parameterless or string constructor (no change)

Solution

  • Easiest solution seems to have been to move to config-file based configuration, as detailed here.

    The reason I couldn't get this to work the first time is because I had a different version of EF listed in one of the various config files and didn't catch it.

    I did try using a single DbConfiguration class in a common library and was able to get it to work this time (with no real fiddling, I must have just done something terribly wrong the first time) but I think that the config-file based configuration is the better solution.

    Putting configuration information in a config file, how novel!