Entity Framework connection factory not working

1.4k views Asked by At

I use entity framework 6 and try to overwrite the connection string factory.

I've written the following connection factory:

internal class MyDBConnectionFactory : IDbConnectionFactory
{
    public System.Data.Common.DbConnection CreateConnection(string nameOrConnectionString)
    {
        SAConnection connection = new SAConnection(ConnectionManager.GetConnectionString(nameOrConnectionString ?? "Default"));

        return connection;
    }
}

Than i have written my own configuration class:

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        // Set provider
        SetProvider();

        // Connection-Information
        SetConnectionInformation();
    }

    private void SetProvider()
    {
        DbProviderServices provider = new iAnywhere.Data.SQLAnywhere.SAProviderServices();

        this.SetProviderServices("iAnywhere.Data.SQLAnywhere", provider);
    }

    private void SetConnectionInformation()
    {
        this.SetDefaultConnectionFactory(new MyDBConnectionFactory());
    }
}

Finally i have added the configuration to the db-context:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class TestDbContext : DbContext
{

}

My problem is, that CreateConnection in MyDBConnectionFactory will never be called and i get the following error message: The underlying provider failed on Open.

Why does my connection will never be created?

Maybe some one has a solution, that would be perfect, thank you.

1

There are 1 answers

1
BendEg On BEST ANSWER

It is important that the configuration section in the app.config does not exists, because it has a higher priority.