If all the
DbContext
classes share the same entities, why not just create a single
DbContext
class, and use the constructor which takes the name of a connection string to create instances pointing to different databases?
public sealed class ErrorsContext : DbContext
{
public ErrorsContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public ISet<Error> Errors
{
get { return Set<Error>(); }
}
}
public sealed class ErrorsContextConfiguration : DbConfiguration
{
public ErrorsContextConfiguration()
{
SetDatabaseInitializer(new NullDatabaseInitializer<ErrorsContext>());
}
}
You can then use a single method to query any one server:
public static IEnumerable<Error> SingleServerQuery(this ErrorsContext context)
{
return context.Errors. ... ;
}
and then call that against each context instance and combine the results:
public static IEnumerable<Error> MultiServerQuery(params ErrorsContext[] servers)
{
return servers.SelectMany(context => SingleServerQuery(context));
}