[英]Entity Framework 4.3 code first database naming
I'm using EF 4.3 with a Code first approach. 我正在使用EF 4.3和Code第一种方法。
With EF 4.3, it recommends using the new <entityFramework />
config section to initialize the connection string for a context. 使用EF 4.3,它建议使用新的<entityFramework />
config部分来初始化上下文的连接字符串。
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. 例如,假设我的应用程序中有一个MyDBContext
实体。 I want it to use the database name specified in the connection string from Initial Catalog=MyDB;
我希望它使用Initial Catalog=MyDB;
的连接字符串中指定的数据库名称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. 使用EF 4.1中的旧方法,我可以通过将连接字符串添加到配置文件中的<connectionstring>
部分来实现这一点。
<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. 如果我想使用EF 4.3支持的更新的配置部分,我无法在连接字符串中指定数据库名称。 I have tried the following but the Initial Catalog
property is ignored. 我尝试了以下内容,但忽略了Initial Catalog
属性。 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 我认为它有理由被忽略,因为整个事情是一个DefaultConnectionFactory
,可以被我的应用程序中的多个上下文使用
<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
我的codefirst方法因使用它而创建的默认数据库名称是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. 我知道有一种方法可以通过将值传递给基本DBContext
对象上的nameOrConnectionString
参数来覆盖此默认命名。
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? 我想知道有没有办法可以方便地从Web.config
文件传递我的数据库上下文的数据库名称,还是应该继续使用<connectionstrings>
部分?
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. 也就是说,我自己没有遇到过这个问题(我很好地硬编码数据库名称 - 它不应该改变)一个选项可能是将你想要的数据库名称存储在Web.config中作为应用程序设置,然后让你的代码从那里拉取值。
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 : 编辑:您可能还想看看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. 根据http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx,这应该仍然适用于4.3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.