EF Ccore 主从配置 最简化
业务需要 配置一主多从数据库 读写分离 orm用的ef core , 把思路和代码写下
1. 配置2个数据库上下文 ETMasterContext ETSlaveContext(把增删改功能禁用掉)
public class ETMasterContext : DbContext
{
public ETMasterContext(DbContextOptions<ETMasterContext> options)
: base(options)
{}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);modelBuilder.ApplyConfiguration(new UserConfiguration());
}
}
public class ETSlaveContext : DbContext
{
public ETSlaveContext(DbContextOptions<ETSlaveContext> options)
: base(options)
{}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);modelBuilder.ApplyConfiguration(new UserConfiguration());
}
public override int SaveChanges()
{
throw new InvalidOperationException("只读数据库,不允许写入");
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
throw new InvalidOperationException("只读数据库,不允许写入");
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
throw new InvalidOperationException("只读数据库,不允许写入");
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
throw new InvalidOperationException("只读数据库,不允许写入");
}}
2. 定义2个Repository EfRepository(主) EfReadOnlyRepository(只读)
public class EfRepository<T> : IRepository<T> where T : EntityBase
{
protected readonly ETMasterContext Context;public EfRepository(ETMasterContext context)
{
Context = context;
//Context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}public IQueryable<T> Table => Context.Set<T>().AsQueryable();
public IQueryable<T> TableNoTracking => Context.Set<T>().AsNoTracking();
public int Delete(T entity)
{
try
{
Context.Set<T>().Remove(entity);
return 1;
}
catch (Exception)
{return 0;
}
}public int DeleteWhere(Expression<Func<T, bool>> criteria)
{
try
{
IQueryable<T> entities = Context.Set<T>().Where(criteria);
foreach (var entity in entities)
{
Context.Entry(entity).State = EntityState.Deleted;
}
return 1;
}
catch (Exception)
{
return 0;
}}
public T GetById(object id)
{
return Context.Set<T>().Find(id);
}public int Insert(T entity)
{
try
{
Context.Set<T>().Add(entity);
return 1;
}
catch (Exception ex)
{
return 0;
}}
public int InsertMany(IEnumerable<T> list)
{
try
{
Context.Set<T>().AddRange(list);
return 1;
}
catch (Exception ex)
{
return 0;
}
}public int Update(T entity)
{
try
{
Context.Entry(entity).State = EntityState.Modified;
return 1;
}
catch (Exception)
{
return 0;
}
}
}
public class EfReadOnlyRepository<T> : IReadOnlyRepository<T> where T : EntityBase
{
protected readonly ETSlaveContext Context;public EfReadOnlyRepository(ETSlaveContext context)
{
Context = context;
//Context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}public IQueryable<T> Table => Context.Set<T>().AsQueryable();
public IQueryable<T> TableNoTracking => Context.Set<T>().AsNoTracking();
public T GetById(object id)
{
return Context.Set<T>().Find(id);
}
}
3. 用到的接口补上
public interface IReadOnlyRepository<T> where T : EntityBase
{
IQueryable<T> Table { get; }IQueryable<T> TableNoTracking { get; }
T GetById(object id);
}public interface IRepository<T> : IReadOnlyRepository<T> where T : EntityBase
{
int Insert(T entity);
int InsertMany(IEnumerable<T> list);
int Update(T entity);
int Delete(T entity);
int DeleteWhere(Expression<Func<T, bool>> criteria);
}
4. Startup ConfigureServices 中配置上下文
services.AddDbContext<ETMasterContext>(options =>
options.UseMySql(GetConnectionStringByRandom("MySql_Master")));
services.AddDbContext<ETSlaveContext>(options =>
options.UseMySql(GetConnectionStringByRandom("MySql_Slave")));
private string GetConnectionStringByRandom(string connectionString)
{
var connstr = Configuration.GetConnectionString(connectionString);
if (string.IsNullOrEmpty(connstr))
{
throw new Exception("数据库配置有误");
}var conList = connstr.Trim('|').Split('|');
var rand = new Random().Next(0, conList.Length);
return conList[rand];
}
5. appsetting.json 配置多个连接字符串 | 分隔
"connectionStrings": {
"MySql_Master": "server=192.168.87.169;database=poker_games;uid=root;pwd=1$=6yuan;SslMode=None;",
"MySql_Slave": "server=192.168.87.169;database=poker_games;uid=root;pwd=1$=6yuan;SslMode=None;|server=192.168.87.169;database=poker_games;uid=root;pwd=1$=6yuan;SslMode=None;"
}
6. 实际应用
public class ReportService : IReportService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IDistributedCache _distributedCache;
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly IReadOnlyRepository<Bet> _betRepository;public ReportService(
IHttpContextAccessor httpContextAccessor,
IDistributedCache distributedCache,
IUnitOfWork unitOfWork,
IMapper mapper,
IReadOnlyRepository<Bet> betRepository
)
{
_httpContextAccessor = httpContextAccessor;
_distributedCache = distributedCache;
_unitOfWork = unitOfWork;
_mapper = mapper;
_betRepository = betRepository;
}}
public ReturnValue GetAgentBetReportByRound(AgentBetReportByRoundCriteriaModel reportModel)
{var betList = _betRepository.TableNoTracking.Where(p => p.pay_out_date >= beginTime && p.pay_out_date < endTime);
return new ReturnValue(betList );}
7. 大概解释下
如图 DBContext的生命周期默认是Scoped,即整个reqeust请求的生命周期以内共用了一个Context
利用这个生命周期 在每次请求时 通过配置文件获取不同的上下文实例 即实现了 多主多从 读写分离功能
EF Ccore 主从配置 最简化的更多相关文章
- mongodb3.x主从配置及备份
本文将介绍下mongodb主从配置及备份 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关 ...
- docker Redis的主从配置
redis是k-v型nosql数据库,支持字符串(string).列表(list).集合(set).散列(hash).有序集合(zset:形如member:score的散列集合,其中member为成员 ...
- CentO7 安装 redis, 主从配置,Sentinel集群故障转移切换
一.Redis的安装(前提是已经安装了EPEL) 安装redis: yum -y install redis 启动/停止/重启 Redis 启动服务: systemctl start re ...
- PostgreSQL9.6主从配置
参考文档: 备机日志传送:https://www.postgresql.org/docs/9.6/static/warm-standby.html 英文文档:https://www.postgresq ...
- MySQL主从配置详解
一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...
- Redis学习总结(四)--Redis主从配置
在分布式系统架构设计中高可用是必须考虑的因素之一.高可用通常是指,通过设计减少系统不能提供服务的时间.而单点是系统高可用的最大的败笔,如果单点出现问题的话,那么整个服务就不能使用了,所以应该尽量在系统 ...
- mysql主从配置实现一主一从读写分离
主从介绍Mysql主从又叫Replication.AB复制.简单讲就是A与B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,实现数据实时同步mysql主从是基于binlog,主上需开启bin ...
- mysql主从配置
引言: 双11,阿里云服务器打折,于是我忍不住又买了一台服务器,于是咱也是有两台服务器的爷们了,既然有了两台服务器,那么肯定要好好利用一下吧,那么就来玩玩mysql的主从配置吧. 准备 两台数据库服务 ...
- postgresql pgsql最新版安装指南及数据存储路径更改及主从配置
postgresql pgsql最新版安装指南及数据存储路径更改及主从配置 安装指南 首先在apt的list添加你当前系统版本对应的apt列表 目前官网有16.04,14.04,12.04 分别对应下 ...
随机推荐
- ffmpeg+cuda+opencv
为了让ffmpeg使用gpu进行解码,可以按以下步骤: 1 下载nvidia官网提供的ffmpeg,编译安装 https://developer.nvidia.com/ffmpeg 注意原来的选项上还 ...
- 弹出table页面--hq
function queryRelation(tableID,prosourceID){ //弹出页面 debugger; initqueryRelationGrid(tableID,prosour ...
- yum安装git
此方法对于RHEL.Fedora.CentOS有效: 1.yum install git 2.yum istall git-svn git-email git-gui gitk
- Python 递归返回树形菜单JSON串 <flask>
需求:菜单管理功能(增.删.改),多级树形菜单展示 数据库表设计 create table if not exists Menu( id serial primary key , title ) no ...
- 安装 fedora 之后
下载zsh,并安装oh-my-zsh dnf install zsh -y 到github的oh-my-zsh上按照教程安装oh-my-zsh,配置主题为minimal,这个是个人比较喜欢的主题,因为 ...
- C#生成二维码的内容
生成二维码的内容 using QRCoder; // 生成二维码的内容 string strCode = this.txtQr.Text.Trim(); if (string.IsNullOrWhit ...
- C# 面向对象多态的抽象性&接口 object&is as类型转换运算符
抽象类/抽象方法 abstract 抽象的数据类型 抽象类不能被实例化 抽象类中不一定存在抽象方法 抽象方法一定是在抽象类中 抽象类里可以放任意的方法 接口 interface 不是类,就是用来当爹 ...
- 快速排序——Java实现
一.排序思想 快速排序是由冒泡排序改进而得到的,是一种分区交换排序方法.思想如下:一趟快速排序采用从两头向中间扫描的方法,同时交换与基准记录逆序的记录. 在待排序的N个记录中任取一个元素(通常取第一个 ...
- js动画杂记
在画布上做动画 方法有 setIntervel(function(){},time); setTimeout(function(){},time); 新方法 window.requestAnimati ...
- javascript实现数据结构:稀疏矩阵的十字链表存储表示
当矩阵的非零个数和位置在操作过程中变化大时,就不宜采用顺序存储结构来表示三元组的线性表.例如,在作“将矩阵B加到矩阵A上”的操作时,由于非零元的插入或删除将会引起A.data中元素的移动.为此,对这种 ...