SqlHelper 使用EF-Core框架 连接池处理并发

2024-08-27 15:28

本文主要是介绍SqlHelper 使用EF-Core框架 连接池处理并发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

定义数据库

数据库名称:T_dicomPatientMsg

注意5大约束条件:

1.主键约束:primary key  IDKEY设置为主键,主键设置自增长

2.唯一性约束:unique

3.默认约束:default    所有值都要设置默认值,除了主键

4.检查约束:check

5.外键约束:foreign key

定义实体
 public class DicomPatientMsg{[Key]public int IDKEY { get; set; }        //设为主键,注意实体名称需要与数据库实体名称一致public string PatientID { get; set; }public string PatientName { get; set; }public DateTime PatientBirthDate { get; set; }public DateTime CheckDate { get; set; }public string PicturePath { get; set; }public string DicomFilePath { get; set; }public string SOPInstanceUID { get; set; }public string StudyID { get; set; }public string StudyInstanceUID { get; set; }public string SeriesInstanceUID { get; set; }public string InstanceNum { get; set; }public bool IsDelete { get; set; } }
SQL帮助类

连接池实现并发连接

public class SqlHelper
{private readonly AppDbContext _context;//构造函数注入DB_contextpublic SqlHelper(AppDbContext context){_context = context;}// 增加实体public async Task AddAsync<T>(T entity) where T : class{await _context.Set<T>().AddAsync(entity);await _context.SaveChangesAsync();}// 获取所有实体public async Task<List<T>> GetAllAsync<T>() where T : class{return await _context.Set<T>().ToListAsync();}// 根据ID获取实体public async Task<T> GetByIdAsync<T>(int id) where T : class{return await _context.Set<T>().FindAsync(id);}// 更新实体public async Task UpdateAsync<T>(T entity) where T : class{_context.Set<T>().Update(entity);await _context.SaveChangesAsync();}// 删除实体public async Task DeleteAsync<T>(T entity, bool isDelete = false) where T : class{if (isDelete){_context.Set<T>().Remove(entity);}else{var property = entity.GetType().GetProperty("IsDeleted");if (property != null && property.PropertyType == typeof(bool)){property.SetValue(entity, true);_context.Set<T>().Update(entity);}else{throw new InvalidOperationException("Error");}}await _context.SaveChangesAsync();}
}
 public async Task AddAsync<T>(T entity) where T : class{await _context.Set<T>().AddAsync(entity);await _context.SaveChangesAsync();}

//增加实体

public 表面方法是公开的,所有其他类都可以调用

async 表方法内可能包含异步操作,允许方法在内部使用"await"

await 在异步操作中使用,会暂停当前方法的执行(阻塞当前线程),直到方法执行完成后,才会继续执行下面的代码,暂停期间,控制权会返回给调用方(如UI线程)

Task 当一个 方法的返回类型是Task时,表面这个方法是异步的,但它不返回任何值(即它是'void'的异步版本,同理int的异步版本为Task<int>)。通过Task,调用者可以选择是否等待这个方法完成

AddAsync<T>(T entity)  T是泛型类型的参数,它使得这个方法可以处理任意类型的实体对象。T由调用者传入的entity类型所决定。如果不使用泛型,只处理某一实体类型如User,也可以写成AddUserAsync(User entity)

where T : class  约束条件,限制了T必须是一个类

 private readonly AppDbContext _context;//构造函数注入DB_contextpublic SqlHelper(AppDbContext context){_context = context;}

这里为什么要用构造函数去注入DbContext

DbContext通常代表数据库的会话(增删改查等),每个DbContext实例都代表与数据库的一次交互。

配置AppDbContext
public class AppDbContext : DbContext
{public AppDbContext(DbContextOptions<AppDbContext> options) : base(options){}// 定义数据库表public DbSet<DicomPatientMsg> T_dicomPatientMsg { get;set;}protected override void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.Entity<DicomPatientMsg>().HasQueryFilter(e => !e.IsDelete);        //过滤已软删除的记录}}
在appsettings.json配置数据库连接字符串
{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning","Microsoft.Hosting.Lifetime": "Information"}},"ConnectionStrings": {"DefaultConnection": "Server=.\\SQLEXPRESS;Database=Colposcope;User Id=sa;Password=123;TrustServerCertificate=True;"},"AllowedHosts": "*"
}//TrustServerCertificate=true  禁用SSL验证
注册DbContext 服务
// 从配置文件读取字符串
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");// 添加DbContext服务
builder.Services.AddDbContext<AppDbContext>((options) => options.UseSqlServer(connectionString));//添加SqlHelper,DicomFunc 和控制器类到 DI 容器
builder.Services.AddScoped<SqlHelper>();
builder.Services.AddScoped<DicomFunc>();
使用数据库
private readonly static SqlHelper sqlHelper = SqlHelper.Instance;DicomPatientMsg msg = new DicomPatientMsg()
{DicomFilePath = patientMsg.DicomFilePath,PatientID = patientMsg.PatientID,PatientName = patientMsg.PatientName,CheckDate = checkDate,PicturePath = patientMsg.PicturePath,SOPInstanceUID = dataset.GetString(DicomTag.SOPInstanceUID),StudyID = dataset.GetString(DicomTag.StudyID),StudyInstanceUID = dataset.GetString(DicomTag.StudyInstanceUID),SeriesInstanceUID = dataset.GetString(DicomTag.SeriesInstanceUID),InstanceNum = dataset.GetString(DicomTag.InstanceNumber),
};//存储到sql
await sqlHelper.AddAsync(msg);

下面理清一下这个数据库的使用流程:

1. 依赖链

 DicomController 依赖 DicomFunc,而 DicomFunc 依赖 SqlHelperSqlHelper 又依赖 AppDbContext

  • DicomController 依赖 DicomFunc
  • DicomFunc 依赖 SqlHelper
  • SqlHelper 依赖 AppDbContext

2. 服务注册

  • AppDbContext 的注册:使用 AddDbContext<AppDbContext> 将数据库上下文注册到 DI 容器中。这允许 SqlHelper 构造函数接收 AppDbContext 实例。
  • SqlHelper 的注册:我们使用 AddScoped<SqlHelper>() 注册 SqlHelper,让 DicomFunc 可以注入它。
  • DicomFunc 的注册:我们注册 DicomFunc,确保 DicomController 能够接收它。

3. 依赖注入的执行

当一个请求到达DicomController时,ASP.NET Core的依赖注入容器会

  1. 实例化 DicomController

    控制器依赖于 DicomFunc,容器会尝试实例化 DicomFunc
  2. 实例化 DicomFunc

    DicomFunc 的构造函数依赖于 SqlHelper,容器会进一步尝试实例化 SqlHelper
  3. 实例化 SqlHelper

    SqlHelper 的构造函数依赖于 AppDbContextAppDbContext 是通过 AddDbContext 方法注册到容器中的,它会被自动提供给 SqlHelper
  4. 实例化 AppDbContext

    容器会从依赖注入容器中提取并实例化 AppDbContext,这可能涉及数据库连接的初始化等操作。
  5. 完成实例化

    • 最终,AppDbContext 实例被注入到 SqlHelper 中。
    • SqlHelper 实例被注入到 DicomFunc 中。
    • DicomFunc 实例被注入到 DicomController 中。

这篇关于SqlHelper 使用EF-Core框架 连接池处理并发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112050

相关文章

Python字符串处理方法超全攻略

《Python字符串处理方法超全攻略》字符串可以看作多个字符的按照先后顺序组合,相当于就是序列结构,意味着可以对它进行遍历、切片,:本文主要介绍Python字符串处理方法的相关资料,文中通过代码介... 目录一、基础知识:字符串的“不可变”特性与创建方式二、常用操作:80%场景的“万能工具箱”三、格式化方法

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req