简体   繁体   中英

Entity Framework 4.3 code first database naming

I'm using EF 4.3 with a Code first approach.

With EF 4.3, it recommends using the new <entityFramework /> config section to initialize the connection string for a context.

I've done some searching online, and I can't seem to find a convenient way to use this method for initializing a connection with a configurable database name.

For example, let say I have a MyDBContext entity in my application. I want it 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 no problem by adding the connection string to the <connectionstring> section in the config 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 newer configsection that EF 4.3 supports, I have no way to specify a database name in the connection string. I have tried the following but the Initial Catalog property is ignored. I believe it is ignored for good reason, because the whole thing is a DefaultConnectionFactory that could 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 DB name that my codefirst approach creates as a result of using this is MyNamespace.MyDBContext

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

So I could do something like this:

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

However, if I take this approach I end up hardcoding DB names into my application and personally I do not like this.

I'm wondering is there any way I can pass a database name for my DB context conveniently from the Web.config file, or should I just continue using the <connectionstrings> section?

How often do you think the database name will have to change?

That said, I haven't run into this issue myself (I'm fine hard-coding the database name - it shouldn't change) one option could be to store the database name you want in the Web.config as an application setting, and then have your code pull the value from there.

EDIT: You may also want to 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 that pulling the connection string with that particular 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 still work in 4.3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM