I have ASP.NET application, which uses several assemblies. Every assembly has own DbContext:
Assembly1.dll - DbContext1 - Configuration1
Assembly2.dll - DbContext2 - Configuration2
Assembly3.dll - DbContext3 - Configuration3
Main ASP.NET application references all these assemblies and tries to connect these contexts to 3 different databases with separate connection strings different per each context.
Configuration for each context is defined in this way (with type name suffixes 1, 2, 3 respectively):
public class Configuration1 : DbConfiguration
{
public Configuration1()
{
SetExecutionStrategy("System.Data.SqlClient",
() => new System.Data.Entity.SqlServer.SqlAzureExecutionStrategy());
System.Data.Entity.Infrastructure.LocalDbConnectionFactory cf =
new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v11.0");
SetDefaultConnectionFactory(cf);
}
}
[DbConfigurationType(typeof(Configuration1))]
public class DbContext1 : DbContext
{
public DbContext1(string connStr) : base(connStr)
{
}
}To my surprise, it does not work. First assembly connection works fine. Then second connection throws this exception:
An instance of 'Configuration1' was set but this type was not discovered in the same assembly as the 'DbContext2' context
Where Configuration1 is configuration defined in the first assembly. Of course, it is not defined in the second assembly. Why this cross reference comes?
This question with very detailed description was already asked here:
http://stackoverflow.com/questions/20354083/ef6-and-multiple-configurations-sql-server-and-sql-server-compact
But it still remains not answered for 3 years. I wonder, if others have ever countered so common situation and how it can be resolved.