Entity Framework connection factory not working

c# connection entity-framework entity-framework-6 sqlanywhere

Question

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
0
6/19/2015 11:19:08 AM

Accepted Answer

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

2
6/19/2015 12:59:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow