Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set manually an Oracle Connection String in a DbContext

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.

Anyone knows how to solve this problem?

like image 587
Afonso França Avatar asked Oct 25 '11 18:10

Afonso França


People also ask

How do I change the connection string in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

How do I find my Oracle connection string?

Under Help -> Support in PL SQL Developer, there is a tab called TNS Names that lists all the available ORACLE SIDs that are used in the application. Save this answer.


1 Answers

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

class MyDbContext: DbContext
{
    public MyDbContext() : base(new OracleConnection("DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM"){}
    ...
}
like image 165
Afonso França Avatar answered Sep 18 '22 10:09

Afonso França