如何通过构造函数传递EF DB First连接字符串,而不是放在app.config中 [英] How to pass a EF DB First connection string via constructor instead put in app.config

查看:80
本文介绍了如何通过构造函数传递EF DB First连接字符串,而不是放在app.config中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发DataBase First EF应用程序,并且自动生成的代码和app.config部分的所有工作都很好。但是,当我尝试删除app.config的连接字符串并将其作为参数放在DbContext构造函数上时,我无法使其正常工作。

I'm developing a DataBase First EF application and everything is working great with the auto-generated code and app.config sections. But when I try to remove the connection string of the app.config and put it as parameter on the DbContext constructor I cannot get it to work.

app.config:

The connection string in the app.config:

  <add name="EntityContext" 
       connectionString="metadata=res://*/ModelHistory.csdl|res://*/ModelHistory.ssdl|res://*/ModelHistory.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=XXXXX;PASSWORD=xxxxx;PERSIST SECURITY INFO=True;USER ID=XXXX&quot;" 
       providerName="System.Data.EntityClient" />

我试图通过构造函数传递它:

And im trying to pass it via constructor:

public EntityContext() : base(GetConnectionString()) { }

private string GetConnectionString()
{
    return metadata=res://*/ModelHistory.csdl|res://*/ModelHistory.ssdl|res://*/ModelHistory.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=XXXXX;PASSWORD=xxxxx;PERSIST SECURITY INFO=True;USER ID=XXXX&quot;" 
}

但是会引发错误:


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

Additional information: 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 ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.


推荐答案

我们需要将连接字符串从app.config文件移动到代码中。这可以在context.cs文件中完成。
在构造函数中,您需要指定连接字符串
在您的连接字符串中,您需要替换替换引用"与'(单引号)

We need to move the connection string from app.config file to the code. This can be done in context.cs file. In the constructor you need to specify the connection string In your connection string you need to replace Replace quote " with '(single quote)

public partial class ProducerCentralEntities : DbContext
    {
        public ProducerCentralEntities()
      : base(@"metadata = res://*/TestProj.csdl|res://*/TestProj.ssdl|res://*/TestProj.msl;provider=System.Data.SqlClient;provider connection string=';data source=serverName;initial catalog=databaseName;integrated security=True;MultipleActiveResultSets=True;application name=EntityFramework';")
        {
        }
      }

注意:
1)您应该具有完整的连接字符串
2)替换引号"

Note: 1) You should have complete connection string 2) Replace quote " with '

这篇关于如何通过构造函数传递EF DB First连接字符串,而不是放在app.config中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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