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:
& 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?
I think your "ConfigurationManager" is null.
Hopefully this snippet, along with the link below will get you on your way. Injection is tidier.
// Import Configuration Extensions
using Microsoft.Extensions.Configuration;
public class CoreDataBase : IDisposable
{
private IDatabase _core = null;
public CoreDataBase()
{
_core = Load();
}
private static Database Load()
{
// Get Instance of Configuration with appsettings.json
IConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
cfgBuilder.AddJsonFile("appsettings.json");
IConfiguration cfg = cfgBuilder.Build();
var connectionString = cfg["ConnectionStrings:Web"];
[...]
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);
}
[...]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.