Entity Framework - Use a dynamic Oracle provider connection string

I need help! I'm not sure that the name is significant enough, so I will try to explain better.

I am working on an angular website that uses http requests for an ASP.NET ASP.NET website. My database is Oracle. I am using Entity Framework (Database First) with a hard-coded 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 the real information with ip; port; name; pwd; usr, since I cannot show them)

It all works, but now my team wants to have this Oracle connection string with dynamic user id and PASSWORD

The goal is to log into my angular application with the same user credentials and connect to Oracle DB with the same credentials. Therefore, I can no longer use this hard connection string. (When they are created, the users are in the db users table and in the oracle user table)

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

public partial class db: DbContext {

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

...}

where connectionString is the same fragment that is in my web.config: "DATA SOURCE = ip: port / name; PASSWORD = pwd; USER SAFETY INFORMATION = True, USER ID = usr"

Testing gave me this error:

"The supplied directory or attachdbfilename oracle is not specified in the supplied sqlconnection"

I think this does not work there because I do not pass my providerName ("Oracle.ManagedDataAccess.Client") in the base first parameter, but I did not find a constructor that accepts the second parameter for the provider name.

I also tried this:

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

but Visual Studio gives me this error:

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

I do not know what DbCompiledModel is, maybe this is the key to this, or maybe I'm wrong.

I also tried editing the web.config connection string, changing the user and password and using the default db constructor, which uses this connection string, but editing this file does not do what I want: the connection string is edited only after the db request is completed, and he reloads the application.

I have an idea that it will be super, I got a little lost there, I just started doing some kind of BackEnd application (I'm on an internship), I used to do FrontEnd.

Thanks for reading, it's a little long. I did not find what I was looking for with successful results on the forums, so I ask you to ask yourself

Sonny

+5
source share
1 answer

You can set dynamically new OracleConnection , but you also need to set contextOwnsConnection to true . This will 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) { } } 
+3
source

Source: https://habr.com/ru/post/1258702/


All Articles