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?