实体框架:动态连接字符串出错 [英] Entity Framework: Error with dynamic connection string

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

问题描述

欢呼所有,

我正在使用实体框架模型。我需要传递我的连接字符串参数,所以在另一个文件中,我写了(从这个问题):

I'm using an Entity Framework model. I need to pass my connection string like parameter, so, in another file, I've written (from 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)
        {

        }
    }
}

当我启动应用程序时,我以这种方式调用这个构造函数,所以我可以在应用程序中反复指出这一点:

When I startup the app, I call this constructor in this way, so I can refer to this from anyway in the app:

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

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

where mdlImpostazioni.SetConnectionString() returns a string (the data are correct):

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

当我执行此代码时,似乎没关系,但是当我尝试查询喜欢:

When I execute this code, it seems to be all ok, but when I try to make a query like:

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

它从这里引发异常:

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

说:


附加信息:如果在Code First模式下使用,则使用数据库优先和模型优先开发的T4模板生成的代码可能无法正常工作。要继续使用Database First或Model First,请确保在运行的应用程序的配置文件中指定了Entity Framework的连接字符串。要使用这些从Database First或Model First生成的类,使用代码首先使用属性或API DbModelBuilder添加任何其他配置,并删除引发此异常的代码。

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.

stacktrace是在c中的MyNamespace.Model.MyEntities.OnModelCreating(DbModelBuilder modelBuilder)中的

the stacktrace is:


:... \Model\Model.Context.cs:System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder)中的行25

在System.Data.Entity.Internal.LazyInternalContext中。 CreateModelBuilder()
在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
在System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)*

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为null。

InnerException is null.

错误在哪里?

推荐答案

我相信你必须更新你的App.config部分

I believe you have to update your App.config section

不确定,但这对我来说是有效的,因为我使用SQLServer不是本地文件。

Not sure but this worked for me as I'm using SQLServer not an local file.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=MyServer; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>

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

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