有人可以帮助我理解以下两种将连接字符串传递给的方法DbContext
?
方法#1:
public EWebDBContextEMS() : base("mainConnectionString")
{
}
和方法#2:
public EWebDBContextEMS() : base("name=mainConnectionString")
{
}
这篇文章指出 that name=...
将由设计师创建,但我用纯测试DbContext
代码,它也有效。
这是有意为之的行为吗?DbContext
构造函数?并且在文档,它没有提到name=
对于连接字符串来说是可以接受的。
非常感谢
类DBContext类注释有完整的解释。
简而言之:
- 您的第一个示例可能会创建一个名为“mainConnectionString”的数据库。
- name=xxxx 在 app.config 中按名称查找连接字符串
某些工具确实会为您将条目放入 App.config 中。
您链接的在线文档准确说明了帮助的位置。
在线帮助说:
使用给定字符串作为名称构造一个新的上下文实例
或将连接到的数据库的连接字符串
制成。请参阅课堂备注,了解如何使用它来创建
联系。
如果你去看课堂笔记,你会找到完整的解释......
/// The connection to the database (including the name of the database) can be specified in several ways.
/// If the parameterless DbContext constructor is called from a derived context, then the name of the derived context
/// is used to find a connection string in the app.config or web.config file. If no connection string is found, then
/// the name is passed to the DefaultConnectionFactory registered on the <see cref="T:System.Data.Entity.Database"/> class. The connection
/// factory then uses the context name as the database name in a default connection string. (This default connection
/// string points to .\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.)
/// Instead of using the derived context name, the connection/database name can also be specified explicitly by
/// passing the name to one of the DbContext constructors that takes a string. The name can also be passed in
/// the form "name=myname", in which case the name must be found in the config file or an exception will be thrown.
/// Note that the connection found in the app.config or web.config file can be a normal database connection
/// string (not a special Entity Framework connection string) in which case the DbContext will use Code First.
/// However, if the connection found in the config file is a special Entity Framework connection string, then the
/// DbContext will use Database/Model First and the model specified in the connection string will be used.
/// An existing or explicitly created DbConnection can also be used instead of the database/connection name.
/// A <see cref="T:System.Data.Entity.DbModelBuilderVersionAttribute"/> can be applied to a class derived from DbContext to set the
/// version of conventions used by the context when it creates a model. If no attribute is applied then the
/// latest version of conventions will be used.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)