0

it seems that the connection to my database is not working at all, but I don't know why.

So you can see the error right there : error

& now let's see the code, appsettings.json :

"ConnectionStrings": {
    "Web": "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xx.xx.x.xxx)(PORT=xxxx))(CONNECT_DATA=(SERVICE_NAME=xxxxxxx)));User Id=xxxxx;Password=xxxxx;"
}

Startup.cs :

public void ConfigureServices(IServiceCollection services)
{
    [...]
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseOracle(Configuration.GetConnectionString("Web")));
    [...]
}

HomeController.cs :

public class HomeController : Controller
{
    [...]
    private readonly CoreDataBase dataBase = new CoreDataBase();
    [...]
}

CoreDataBase.cs :

public class CoreDataBase : IDisposable
{
    private IDatabase _core = null;

    public CoreDataBase()
    {
        _core = Load();
    }

    private static Database Load()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
        var provider = ConfigurationManager.ConnectionStrings["Web"].ProviderName;
        if(provider != "Oracle.ManagedDataAccess.Client")
        {
            return null;
        }
        return new Database(connectionString, DatabaseType.OracleManaged, SqlClientFactory.Instance);
    }
    [...]
}

Do you have any idea ?

3
  • ConfigurationManager don't get value from appsettings.json, but from App.config or Web.config. You need rewrite CoreDataBase to inject connection string settings. Commented Dec 16, 2020 at 9:38
  • start with using configuration extensions instead of configuration manager. Commented Dec 16, 2020 at 9:41
  • 1
    Please note that ASP.NET Core use different configuration settings, the default file that stores configuration data for the application used in the ASP.NET Core project templates is appsettings.json, not same as the Web.config used in ASP.NET project. You can read these data from appsettings.json file via an instance of IConfiguration that you injected. Commented Dec 16, 2020 at 10:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.