-
Notifications
You must be signed in to change notification settings - Fork 455
Oracle DbContext
shuxin edited this page Dec 2, 2023
·
6 revisions
对于 Oracle 数据库,需要安装 Install-Package Chloe.Oracle 以及 Oracle 的驱动,然后使用 Chloe.Oracle.OracleContext 创建上下文实例。注意:DbContext 实例非线程安全,一定要避免多线程同时使用同一个 DbContext 对象。同时,用完务必要将 DbContext 释放。
创建 OracleContext:
OracleContext context = new OracleContext(() =>
{
OracleConnection oracleConnection = new OracleConnection("Your connection string"); //需要自己引入数据库驱动
OracleConnectionDecorator conn = new OracleConnectionDecorator(oracleConnection); //OracleConnection 装饰器
return conn;
});
OracleContext 生成 sql 语句时默认将表名和字段转成大写形式,如需要修改该默认设置,操作如下:
context.Options.ConvertToUppercase = false;
ASP.NET CORE 配置 Service:
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddScoped<Chloe.IDbContext>((serviceProvider) =>
{
OracleContext context = new OracleContext(() =>
{
OracleConnection oracleConnection = new OracleConnection("Your connection string"); //需要自己引入数据库驱动
OracleConnectionDecorator conn = new OracleConnectionDecorator(oracleConnection); //OracleConnection 装饰器
return conn;
});
return context;
});
//...
}
由于笔者使用的是 Oracle.ManagedDataAccess 数据库驱动,OracleConnection 创建的 DbCommand 默认是以顺序方式绑定参数,所以,上述例子使用了装饰者模式对 OracleConnection 封装了一遍,主要就是修改 DbCommand 参数绑定方式。OracleConnectionDecorator 定义如下:
/// <summary>
/// 该装饰器主要修改参数绑定方式。
/// </summary>
class OracleConnectionDecorator : DbConnectionDecorator, IDbConnection, IDisposable
{
OracleConnection _oracleConnection;
public OracleConnectionDecorator(OracleConnection oracleConnection) : base(oracleConnection)
{
this._oracleConnection = oracleConnection;
}
public override IDbCommand CreateCommand()
{
var cmd = this._oracleConnection.CreateCommand();
cmd.BindByName = true; //修改 DbCommand 参数绑定方式
return cmd;
}
}
熬夜的时候容易引起脱发,熬夜时人的免疫力会下降,还会引起内分泌的变化,导致皮脂分泌增多,这些因素都不利于头发的生长,有可能引起脱发。如果出现熬夜脱发的情况,需要及时纠正日常的生活习惯,合理安排休息时间,早睡早起,适当的锻炼身体,多进食优质的蛋白质,增强身体的抵抗力,促进头发的生长。
发量有限,远离996!!!