Skip to content

KingbaseES DbContext

shuxin edited this page Dec 8, 2023 · 2 revisions

创建 DbContext

对于人大金仓数据库,需要安装 Install-Package Chloe.KingbaseES 以及 KingbaseES的驱动,然后使用 Chloe.KingbaseES.KingbaseESContext 创建上下文实例。注意:DbContext 实例非线程安全,一定要避免多线程同时使用同一个 DbContext 对象。同时,用完务必要将 DbContext 释放。

创建 DamengContext:

KingbaseESContext context = new KingbaseESContext(() =>
{
    IDbConnection conn = new KdbndpConnection("Your connection string");  //需要自己引入数据库驱动
    return conn;
});

ASP.NET CORE 配置 Service:

public void ConfigureServices(IServiceCollection services)
{
    //...
	
    services.AddScoped<Chloe.IDbContext>((serviceProvider) =>
    {
        KingbaseESContext context = new KingbaseESContext(() =>
        {
            IDbConnection conn = new KdbndpConnection("Your connection string");  //需要自己引入数据库驱动
            return conn;
        });

        return context;
    });
   
    //...
}