如何在 DbContext 中手动设置 Oracle 连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7894086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:24:04  来源:igfitidea点击:

How to set manually an Oracle Connection String in a DbContext

asp.net-mvcoracleef-code-first

提问by Afonso Fran?a

I have the following connection string:

我有以下连接字符串:

<add name="DataContext" connectionString="DATA SOURCE=Server;
PASSWORD=123;USER ID=SYSTEM" providerName="Oracle.DataAccess.Client"/>

My business logic determine I need to read manually the connection string of the database:

我的业务逻辑决定我需要手动读取数据库的连接字符串:

class MyDbContext: DbContext
{
    public MyDbContext() : 
    base(ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString){}
    ...
}

It works properly with Sql Server but when I change to a Oracle Connection string don't works. It happens because the DbContext tries to use the Oracle ConnectionString to connect on a Sql Server Database because it dinn't receive the providerName.

它与 Sql Server 一起正常工作,但是当我更改为 Oracle 连接字符串时不起作用。这是因为 DbContext 尝试使用 Oracle ConnectionString 连接 Sql Server 数据库,因为它没有收到 providerName。

Anyone knows how to solve this problem?

有谁知道如何解决这个问题?

采纳答案by Afonso Fran?a

To create a DbContext using Oracle without use WebConfig, your inheritance of DbContext must inject an Oracle Connection to base constructor:

要在不使用 WebConfig 的情况下使用 Oracle 创建 DbContext,您对 DbContext 的继承必须将 Oracle 连接注入到基本构造函数:

class MyDbContext: DbContext
{
    public MyDbContext() : base(new OracleConnection("DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM"){}
    ...
}

回答by Gustavo Rubio

Just found the solution after struggling with this all afternoon. Seems that the constructor or DbContext will use either the connection string or connection string name, which is not the same, if you pass a connection string it will default to SqlClient which is the only thing bundled by default in .NET

经过一整个下午的努力,才找到了解决方案。似乎构造函数或 DbContext 将使用连接字符串或连接字符串名称,这是不一样的,如果您传递连接字符串,它将默认为 SqlClient,这是 .NET 中默认捆绑的唯一内容

Now, if you, instead of using the whole connection string, pass just the connection string name then it will internally parse also the "providerName" parameter which has the assembly name for the DB provider Oracle.DataAccess.Client for instance.

现在,如果您不使用整个连接字符串,而只传递连接字符串名称,那么它还会在内部解析“providerName”参数,该参数具有数据库提供程序 Oracle.DataAccess.Client 的程序集名称,例如。

So instead of passing the connection string to the DbContext constructor just pass the connection string name, like this:

因此,不要将连接字符串传递给 DbContext 构造函数,只需传递连接字符串名称,如下所示:

.config file:

.配置文件:

<connectionStrings>
    <add name="SQLServer" connectionString="Server=localhost; Database=MyDb; User ID=MyUser;Password=MyPwd;Pooling=false" providerName="System.Data.SqlClient" />
    <add name="Oracle" connectionString="Data Source=localhost:1521/XE;Persist Security Info=True;User ID=MyUser;Password=MyPwd;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
<appSettings>
    <add key="DefaultConnection" value="Oracle" />
</appSettings>

And in your DbContext:

在你的 DbContext 中:

public MyDbContext()
    : base("DefaultConnection")
{

}

That way you just set up a config key with the name of the connection string you want to hook the context to and use it in the constructor. If you do it this way EF will automatically parse the whole connection string tag and not only the connectionString attribute value, hence, load the right provider.

这样,您只需使用您想要将上下文挂钩的连接字符串的名称设置一个配置键,并在构造函数中使用它。如果您这样做,EF 将自动解析整个连接字符串标记,而不仅仅是 connectionString 属性值,因此,加载正确的提供程序。

Do note that I'm using Oracle.ManagedDataAccess.Client which is newer and only included in ODAC / ODP.NET v12 and above. If you use ODAC 11 you should use Oracle.DataAccess.Client in providerName instead.

请注意,我使用的是较新的 Oracle.ManagedDataAccess.Client,仅包含在 ODAC/ODP.NET v12 及更高版本中。如果您使用 ODAC 11,则应在 providerName 中使用 Oracle.DataAccess.Client。

回答by the

Got it working by specifying "Default Connection Factory" in web/app .config

通过在 web/app .config 中指定“默认连接工厂”来让它工作

<configuration>
...
    <entityFramework>
        <defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework" />
        <providers>
          ...

https://docs.oracle.com/cd/E63277_01/win.121/e63268/entityCodeFirst.htm

https://docs.oracle.com/cd/E63277_01/win.121/e63268/entityCodeFirst.htm