具有连接字符串的Entity Framework DbContext构造函数

时间:2014-06-19 13:54:51

标签: entity-framework

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

方法#1:

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

和方法#2:

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

This article states name=...将由设计师创建,但我使用纯DbContext代码进行测试,它也能正常运行。

这是DbContext构造函数的意图吗?在documentation中,没有提到连接字符串可以接受name=

非常感谢

2 个答案:

答案 0 :(得分:6)

类DBContext类备注有完整的解释 简而言之:

  • 您的第一个示例可能会导致名为" mainConnectionString"被创造。
  • name = xxxx在app.config中查找名称为
  • 的connectionStrings

某些工具确实为您添加了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.

答案 1 :(得分:2)

第一种方法允许您在运行时创建自己的完整连接字符串,而不是app.config或web.config文件中的命名连接字符串。

方法2在配置文件中使用命名连接字符串。

使用方法1和处理连接字符串构建的部分类,您可以在运行时构建连接字符串,如:

DbContext构造函数接受连接字符串作为参数。 你可能最好建立一个连接字符串并将其传递给构造函数,如下所示:

我使用了类似的东西:

// the model name in the app.config connection string (any model name - Model1?)
private static string GetConnectionString(string model, settings)
{
    // Build the provider connection string with configurable settings
    var providerSB = new SqlConnectionStringBuilder
    {
        InitialCatalog = settings.InitialCatalog,
        DataSource = settings.DataSource,
        UserID = settings.User,
        Password = settings.Password
    };

    var efConnection = new EntityConnectionStringBuilder();
    // or the config file based connection without provider connection string
    // var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
    efConnection.Provider = "System.Data.SqlClient";
    efConnection.ProviderConnectionString = providerSB.ConnectionString;
    // based on whether you choose to supply the app.config connection string to the constructor
    efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model); ;
    return efConnection.ToString();

}