[英]How to read appsettings.json in any Class with .Net core 5
I have this class that connects to my oracle database:我有这个 class 连接到我的 oracle 数据库:
public class Conexao : IDisposable
{
private readonly IOptions<Configuracao> _config;
public OracleConnection ConexaoOra{ get; internal set; }
private OracleTransaction _transacao;
private bool _finalized;
private bool _commited;
private const string CONNECTION_STRING = "myConnectionString";
public Conexao(IOptions<Configuracao> config)
{
_config = config;
_finalized = false;
_commited = false;
ConexaoOra = new OracleConnection();
ConexaoOra.ConnectionString = CONNECTION_STRING;
ConexaoOra.Open();
}
~Conexao()
{
Dispose();
}
public void BeginTransaction(System.Data.IsolationLevel level = System.Data.IsolationLevel.ReadCommitted)
{
_transacao = ConexaoOra.BeginTransaction(level);
}
public void Commit()
{
if (_transacao != null)
{
_transacao.Commit();
_transacao = null;
}
_commited = true;
}
}
This class is instantiated every time any transaction is triggered in the DAL class每次在 DAL class 中触发任何事务时,都会实例化此 class
sample:样本:
using ((conexao == null ? conexao = new Conexao() : conexao))
OracleCommand oracleCommand = new OracleCommand("", conexao.ConexaoOra);
//...
The Json Settings File and the Connection Class are in different projects in the same solution Json 设置文件和连接 Class 在同一解决方案的不同项目中
how to load in this line my connectionString of appSettings Json File?如何在此行中加载我的 appSettings Json 文件的连接字符串?
private const string CONNECTION_STRING = "myConnectionString";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.