Change connection string in EF6 based on provider from config

c# entity-framework-6 orm

Question

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.

1
1
9/11/2013 9:44:30 AM

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,
                                                        @"c:\test",
                                                        @"c:\test\db.sdf"));
SetProviderServices(SqlCeProviderServices.ProviderInvariantName,
                    SqlCeProviderServices.Instance);
}

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

3
12/2/2013 4:59:36 AM


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