Sonny Jayet Sonny Jayet - 1 month ago 20
ASP.NET (C#) Question

Entity Framework - Use Dynamic connection string with Oracle Provider

I need help ! I'm not sure the title is significant enought so i will try to explain better.

I work on an angular web site that uses http requests to a ASP.NET web api site.
My database is an Oracle one.
I use Entity Framework ( Database First ) with an hardcoded connection string ( in my web.config )


<add name="UserConnection" connectionString="DATA SOURCE=ip:port/name;PASSWORD=pwd;PERSIST SECURITY INFO=True;USER ID=usr"
providerName="Oracle.ManagedDataAccess.Client" />


( I replaced real infos with ip;port;name;pwd;usr since I can't show them )


All of this is working, but now my team wants to be able to have this Oracle connection string with dynamic USER ID and PASSWORD

The goal is to log in on my angular app with one user's credentials and to connect to Oracle DB with the same credentials. Therefore i can't use this hardcoded connection string anymore. ( When created, users are both in db users table and in oracle users table )

I tried to create another constructor for my db : Dbcontext class, giving a connection string in : base () first parameter :


public partial class db: DbContext {

public db(string connectionString) : base(connectionString) {}

... }


where connectionString is the same piece that is in my web.config : "DATA SOURCE=ip:port/name;PASSWORD=pwd;PERSIST SECURITY INFO=True;USER ID=usr"

Testing it gave me this error :


"The supplied sqlconnection does not specify an initial catalog or attachdbfilename oracle"


I think this is not working there because i don't give my providerName ("Oracle.ManagedDataAccess.Client") in base first param, but i didn't find a constructor which accepts a second param for providerName.

I also tried this :


public db(string connectionString) : base(new OracleConnection(connectionString)) {}


but there Visual Studio gives me that error :


" Erreur CS1503 Argument 1 : conversion impossible de 'Oracle.ManagedDataAccess.Client.OracleConnection' en 'System.Data.Entity.Infrastructure.DbCompiledModel' "


I don't know what a DbCompiledModel is, maybe it's the key to this, or maybe i'm doing completely wrong.

I also tried to edit web.config connection string, to change user and password and use default db constructor which uses this connection string, but editing this file doesn't do what i want: the connection string gets edited only after the db request is finished, and it reloads the app.

I you have any idea it would be super, i'm a bit lost there, i just started doing some BackEnd app ( i'm in an internship ) i was only doing FrontEnd before.

Thanks for reading, it's a bit long. I didn't find what i was looking for with succesful results in forums so I try asking by myself

Sonny

mww mww
Answer

You can set dynamically new OracleConnection, but you need also set contextOwnsConnection to true. This resolve your error :

" Erreur CS1503 Argument 1 : conversion impossible de 'Oracle.ManagedDataAccess.Client.OracleConnection' en 'System.Data.Entity.Infrastructure.DbCompiledModel' "

public partial class Entities : DbContext
    {
        public Entities()
            : base(new OracleConnection("DATA SOURCE=Server; PASSWORD=123;USER ID=SYSTEM"), true)
        {

        }
      }
Comments