Entity Framework 4.3 code identification of the first database

I am using EF 4.3 with the first code approach.

In EF 4.3, it is recommended that you use the new <entityFramework /> configuration section to initialize the connection string for the context.

I have done several searches on the Internet and I cannot find a convenient way to use this method to initialize a connection with a custom database name.

For example, suppose I have a MyDBContext object in my application. I want him to use the database name specified in the connection string from Initial Catalog=MyDB;

Using the old method from EF 4.1, I can do this without problems by adding a connection string to the <connectionstring> section in the configuration file.

 <add name="MyDBContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/> 

If I want to use the new configuration supported by EF 4.3, I have no way to specify the database name in the connection string. I tried the following, but the Initial Catalog property is ignored. I believe this is ignored for a good reason, because all of this is a DefaultConnectionFactory that can be used by multiple Contexts in my application

 <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> <parameters> <parameter value="Data Source=.\SQLEXPRESS; Initial Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True" /> </parameters> </defaultConnectionFactory> </entityFramework> 

The default database name created by my codefirst method as a result of using MyNamespace.MyDBContext

I understand that there is one way to rewrite this default name by passing a value to the nameOrConnectionString argument on the nameOrConnectionString base object.

So I could do something like this:

 public MyDBContext() : base("MyDB") { } 

However, if I use this approach, I end up DB hard drive names in my application, and I personally don't like it.

I am wondering if it is possible to somehow pass the database name for my DB context from the Web.config file, or just continue using the <connectionstrings> section?

+7
source share
1 answer

How often do you think the database name should change?

However, I myself did not encounter this problem (I am well versed in the database name - it should not change) one option would be to save the database name that you want in Web.config as application settings, and then your code pull the value from there.

EDIT: you can also take a look at http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models .aspx :

 public UnicornsContext() : base("name=UnicornsCEDatabase") { } 

With this, pulling the connection string with this specific name:

 <configuration> <connectionStrings> <add name="UnicornsCEDatabase" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=Unicorns.sdf"/> </connectionStrings> </configuration> 

According to http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx this should work in 4.3.

0
source

All Articles