【问题标题】:Entity Framework DbContext constructor with connection string具有连接字符串的实体框架 DbContext 构造函数
【发布时间】:2014-09-26 00:52:20
【问题描述】:

谁能帮我理解以下两种将连接字符串传递给DbContext的方法?

方法一:

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

和方法#2:

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

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

这是DbContext 构造函数的有意行为吗?而在documentation 中,并没有提到name= 可以用于连接字符串。

非常感谢

【问题讨论】:

    标签: entity-framework


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,它指出了我在那篇文章中遗漏的确切陈述。
    • “看课堂评论”,“如果你去课堂评论......”
    • @redwards510 我使用 Resharper dotpeek 来反编译源代码。它是免费安装的。这是一个免费软件程序。但是还有其他工具,例如 .net reflector
    【解决方案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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      相关资源
      最近更新 更多