Change connection string in EF6 based on provider from config


I have moved to EntityFramework 6 from EF5.

Now I am facing an issue, I want to change the connection string based the provider set in app.Config. Meaning If it is SqlServerCe, then I need to change the DatabaseFile.sdf file name to change a database. If it is SqlServer, the I need to change InitialCataloge.

I did this by getting the type of

'Database.DefaultConnectionFactory' but this is Obsolete in EF6.

How can I modify the connection string based on provider.

I cannot do this by using DbConfiguration Attribute since I need to modify connection string everytime.

Accepted Answer

You can use

DbConfiguration.SetConfiguration(new YourDbConfiguration());

so you can use your config without using an attribute.

But if I use

public CeDbConfiguration()
 SetDefaultConnectionFactory(new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName,

to to configure a SQL CE Factory, the path 'C:\test' will be ignored an I have to set Database.DefaultConnectionFactory also!!

Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why