c# - Entity Framework : Error with dynamic connection string

标签 c# database entity-framework database-connection connection-string

大家干杯

我正在使用 Entity Framework 模型。我需要传递连接字符串之类的参数,因此,在另一个文件中,我编写了(来自 this question ):

namespace MyNamespace.Model
{
    using System;
    using System.Data.Entity;
     using System.Data.Entity.Infrastructure;

    public partial class MyEntities: DbContext
    {
        public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
        {

        }
    }
}

当我启动应用程序时,我以这种方式调用此构造函数,因此我可以在应用程序中以任何方式引用它:

public static MyEntities dbContext = 
                         new MyEntities(mdlImpostazioni.SetConnectionString());

其中mdlImpostazioni.SetConnectionString()返回一个字符串(数据正确):

server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;

当我执行这段代码时,似乎一切正常,但是当我尝试进行如下查询时:

var query = (from r in MainWindow.dbContext.TabTipoSistema select r);

它从这里抛出异常:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

说:

Additional information: The code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First, make sure that the connection string of Entity Framework is specified in the configuration file of the application running. To use these classes, which were generated from Database First or Model First, with Code First add any additional configuration using attributes or the API DbModelBuilder and remove the code that throws this exception.

堆栈跟踪是:

in MyNamespace.Model.MyEntities.OnModelCreating(DbModelBuilder modelBuilder) in c:...\Model\Model.Context.cs:row 25 in System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder) in System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder() in System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) in System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)*

InnerException 为空。

哪里出错了?

最佳答案

您不能仅通过构造函数使用简单类型连接字符串作为 Entity Framework 6 连接字符串。 我只是使用以下函数来获取实体兼容的函数。

不要忘记将“YourDBModel”更改为您正在使用的任何模型。

    /// <summary>
    /// Returns an Entity Connectionstring ready to be used in a EF6 contructor
    /// </summary>
    /// <param name="connectionString">plain connectionstring</param>
    /// <returns>entity connectionstring</returns>
    public string GetEntityConnectionString(string connectionString)
    {
        var entityBuilder = new EntityConnectionStringBuilder();

        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = connectionString;

        // -- WARNING --
        // Check your app config and set the appropriate DBModel (do not just let this be YourDBModel) 
        entityBuilder.Metadata = @"res://*/DB.YourDBModel.csdl|res://*/DB.YourDBModel.ssdl|res://*/DB.YourDBModel.msl";

        var ret = entityBuilder.ToString();

        return ret;
    }

您可能还想将以下内容添加到您的连接字符串

entityBuilder.ProviderConnectionString = connectionString;  + ";MultipleActiveResultSets=True;App=EntityFramework;";

关于c# - Entity Framework : Error with dynamic connection string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26116697/

相关文章:

c# - .NET 框架中的 lambda 是如何解析的?

c# - 无法将数组转换为 IEnumerable

javascript - 使用 Node JS 在 mongoose 中存储多个文本的理想方法是什么?

c# - EntityContainer 中未定义 EntitySet 'sproc_Forums_GetForumGroupByID'

c# - 更新成员(member)属性。不支持的方法

c# - 查找参数化查询的 SQL 输出

database - SELECT DISTINCT 是否意味着 Seq Scan?

Android 应用程序数据库数据不会显示(从本地主机检索 JSON 数据)

c# - 如何使用 Entity Framework 4 Code First (POCO) 声明一对一关系

c# - 使用 LINQ to Entity Framework 从多个不相关的表中选择数据