23

Update: Problem solved, see end of this question.

The problem:

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 AppDomain.

This quite simple scenario seems not to be supported "by design". From the EF team:

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.

More information here: Code-based Configuration (Codeplex)

The question:

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 AppDomain?

(Our context classes are located in different assemblies. We have tried the DbConfigurationType attribute but the problem is EF itself)

Configuration files:

Configuration for normal SQL server

public class EfConfiguration : DbConfiguration
{
    public EfConfiguration()
    {
        SetProviderServices(
            SqlProviderServices.ProviderInvariantName, 
            SqlProviderServices.Instance);

        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}

Configuration for SQL Server Compact Edition

public class EfCeConfiguration : DbConfiguration
{
    public EfCeConfiguration()
    {
        SetProviderServices(
            SqlCeProviderServices.ProviderInvariantName,
            SqlCeProviderServices.Instance);

        SetDefaultConnectionFactory(
            new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName));
    }
}

UPDATE:

The error which we get is:

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 http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

UPDATE 2, the solution 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.

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.

  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);
        }
    }
10
  • I'm a bit confused -- I have two databases in two separate C# projects (assemblies) each with their own configuration and have never had any issues whatsoever.
    – Kirk Woll
    Dec 3, 2013 at 14:42
  • Kirk: Are you using EF6 and do you execute them in the same App-Domain? We use nunit and are performing an integration test across two systems but running them as one. Worked fine in EF5 where one didn't have to specify the provider in configuraton. Dec 3, 2013 at 14:56
  • Yes, and yes. What actual error are you experiencing?
    – Kirk Woll
    Dec 3, 2013 at 14:58
  • @kirk Discovery of the DBConfiguration if it is used is the issue. Perhaps your scenario has all pieces in the correct assembly for auto discovery. If DBConfig classes to be used are in the same assembly :-( gets nasty
    – phil soady
    Dec 3, 2013 at 15:00
  • @kirk: Gives me hope that you have solved this! :) I've posted the error in an update in the question. Dec 3, 2013 at 15:05

3 Answers 3

13

EDIT: based On Error details: Did you already try tell EF where the config class is found?

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssemblyFullyQualifiedName")]
public class MyContextContext : DbContext
{
}

If that cant be made work, then see alternative

Use the Context with constructor DbConnection

public class MYDbContext : DbContext {
     // MIgration parameterless constructor is managed in  MyMigrationsContextFactory 

    public MyDbContext(string connectionName) : base(connectionName) { } // no this

    public MYDbContext(DbConnection dbConnection, bool contextOwnsConnection)  // THIS ONE
        : base(dbConnection, contextOwnsConnection) {  }

you then need a "DBConnection" connection for each provider. For SQL server

      public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }

repeat for SqlCe factory, it can also generate a DBConnection SqlCe connection factor create connection

6
  • Yes I did try to specify it for both configuration files. That's the message I got. Dec 3, 2013 at 15:35
  • Thanks man! We already had you concept for the CE-connection but for the regular connections we used the connection string. When we instead supplied a connection to the context the configuration was not necessary anymore! (Except for the provider definition) Dec 3, 2013 at 17:01
  • 1
    You could mention that MyAssembly has to be a fully qualified name. Dec 24, 2014 at 20:06
  • @phil soady do you know about oracle provide?
    – Amir
    Nov 3, 2016 at 23:27
  • I have never used Oracles EF provider. Hit their website to find out more. oracle.com/technetwork/topics/dotnet/downloads/…
    – phil soady
    Nov 4, 2016 at 21:03
2

what i did:

public partial class MyDataBaseContext : DbContext
{
    public MyDataBaseContext (string ConnectionString)
        : base(ConnectionString)
    {
    }
}
2
  • 1
    with something like var ConnectionString = @"Data Source="C:\temp\mydatabase.sdf" this works fine. Apr 29, 2015 at 20:51
  • From my MVC HomeController class, I did this: public static readonly string conStr = ConfigurationManager.ConnectionStrings["SudentConext"].ToString(); MyDataBaseContext db = new MyDataBaseContext(conStr); Apr 11, 2016 at 23:35
-1

I found the solution in a post on a Microsoft forum post.

Basically, I had two projects, each one with its own context. Entity Framework was loading just (the first) one of the DbConfiguration classes and trying to use this same configuration for both projects. That's the reason for the error message saying something like

"An instance of 'EfCeConfiguration' was set but this type was not discovered in the same assembly as the 'DataContext' context".

So, as someone suggested in that Microsoft forum post, I removed all [DbConfigurationType(typeof(DbConfigurationClass))] anotations from the classes which inherit from DbContext in both projects, and the error didn't happen anymore.

2
  • 3
    But did it run after that? I mean, the DbConfiguration subclass you use is kind of important to it functioning correctly at runtime. It configures a lot of database-engine-specific things (e.g. completely different settings for MySQL vs MSSQL) like the dependency resolver (eg. MySqlDependencyResolver), provider factory (e.g. MySqlClientFactory), provider services (e.g. MySqlProviderServices), connection factory (e.g. MySqlConnectionFactory), migration generator, provider factory resolver, manifest token resolver, and history context. You can't just not specify all that and have it work.
    – Triynko
    Apr 17, 2019 at 19:15
  • I dunno, bro. I don't remember the details of the project I was working at when I answered it, but I'm positive it solved the issue at hand at that time. Now, to check the things you are pointing out, I think you'll be better able to test it because I don't have the environment with this problem happening anymore. Apr 17, 2019 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.