title: EF6 and multiple configurations (SQL Server and SQL Server Compact)
body: <p><strong>Update:</strong> Problem solved, see end of this question.</p>
  
  <p><strong>The problem</strong>:</p>
  
  <p>We are trying to use Entity Framework 6 and code-based configuration in a scenario were we have use both a SQL Server and SQL Server CE in the same <code>AppDomain</code>.</p>
  
  <p>This quite simple scenario seems not to be supported "by design". From the EF team:</p>
  
  <blockquote>
    <p>Note: We do not support having multiple configuration classes used in
    the same AppDomain. If you use this attribute to set different
    configuration classes for two contexts an exception will be thrown.</p>
  </blockquote>
  
  <p>More information here: <a href="http://entityframework.codeplex.com/wikipage?title=Code-based%20Configuration" rel="noreferrer">Code-based Configuration (Codeplex)</a></p>
  
  <p><strong>The question</strong>:</p>
  
  <p>How do we move forward from here? Any help would be greatly appreciated! Is there a more flexible way to connect a configuration to a context instead of an <code>AppDomain</code>?</p>
  
  <p>(Our context classes are located in different assemblies. We have tried the <code>DbConfigurationType</code> attribute but the problem is EF itself)</p>
  
  <p>Configuration files:</p>
  
  <p>Configuration for normal SQL server</p>
  
  <pre><code>public class EfConfiguration : DbConfiguration
  {
      public EfConfiguration()
      {
          SetProviderServices(
              SqlProviderServices.ProviderInvariantName, 
              SqlProviderServices.Instance);
  
          SetDefaultConnectionFactory(new SqlConnectionFactory());
      }
  }
  </code></pre>
  
  <p>Configuration for SQL Server Compact Edition</p>
  
  <pre><code>public class EfCeConfiguration : DbConfiguration
  {
      public EfCeConfiguration()
      {
          SetProviderServices(
              SqlCeProviderServices.ProviderInvariantName,
              SqlCeProviderServices.Instance);
  
          SetDefaultConnectionFactory(
              new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName));
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The error which we get is:</p>
  
  <blockquote>
    <p>System.TypeInitializationException : The type initializer for
    'MyProject.Repositories.Base.DataContext'
    threw an exception.   ----> System.InvalidOperationException : An
    instance of 'EfCeConfiguration' was set but this type was not
    discovered in the same assembly as the 'DataContext' 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. See <a href="http://go.microsoft.com/fwlink/?LinkId=260883" rel="noreferrer">http://go.microsoft.com/fwlink/?LinkId=260883</a> for
    more information.</p>
  </blockquote>
  
  <p><strong>UPDATE 2, the solution</strong>
  As described above, we can only have one configuration. This is a problem since Sql and SqlCe uses different providers. If we use "SetDefaultConnectionFactory" to fit one type of database, the other will fail.</p>
  
  <p>Instead, supply the connection into the context as described in the post marked as answer below. Once you always initialize the context with a connection as opposed to a connectionstring you are good to go. You can remove the SetDefaultConnectionFactory call from the configuration. We're using only the code below for configuring the SqlCe Context and no configuration for the Sql Context.</p>
  
  <pre><code>  public class CommonEfConfiguration : DbConfiguration
      {
          public CommonEfConfiguration()
          {
              // EF does not know if the ce provider by default,
              // therefore it is required to be informed about it.
              // The connection factories are not necessary since the connection
              // is always created in the UnitOfWork classes
              SetProviderServices(SqlCeProviderServices.ProviderInvariantName, SqlCeProviderServices.Instance);
          }
      }
  </code></pre>