实体框架DbContext构造函数与连接字符串 [英] Entity Framework DbContext constructor with connection string

查看:140
本文介绍了实体框架DbContext构造函数与连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解以下两种方法将连接字符串传递给 DbContext

Can someone help me understand the following two approaches to pass a connection string to the DbContext?

方法#1:

public EWebDBContextEMS() : base("mainConnectionString")
{
}

和方法#2:

public EWebDBContextEMS() : base("name=mainConnectionString")
{
}

本文说明 name = ... 将由设计者创建,但是我用纯 DbContext 代码测试,它也可以工作。

This article states that name=... will get created by designer, but I tested with pure DbContext code, it works as well.

这是否是 DbContext 构造函数的有意行为?而在文档中,它会没有提到连接字符串可以接受 name =

Is this an intented behaviour of the DbContext constructor? And in the documentation, it does not mention that name= is acceptable for connection string.

非常感谢

推荐答案

类DBContext Class说明。

简而言之:

The class DBContext Class remarks have the full explanation.
In short:


  • 您的第一个例子可能会导致一个名为mainConnectionString的数据库被创建。 / li>
  • name = xxxx通过名称查找connectionStrings的app.config

某些工具会将条目在您的App.config中。

您链接的在线文档说明了帮助的准确位置。

Certain tools do put entries in App.config for you.
The online documentation you linked states exactly where the help is.

在线帮助说:


使用给定的字符串作为名称
或连接到
的数据库的连接字符串构造一个新的上下文实例制作。查看课程说明,了解如何使用这种方式创建
连接。

Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made. See the class remarks for how this is used to create a connection.

如果你去课堂讲话,你将会找到一个完整的解释....

If you go to the class remarks you will find a full explanation....

///               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.

这篇关于实体框架DbContext构造函数与连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆