业务需要 配置一主多从数据库 读写分离  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 主从配置 最简化的更多相关文章

  1. mongodb3.x主从配置及备份

    本文将介绍下mongodb主从配置及备份 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关 ...

  2. docker Redis的主从配置

    redis是k-v型nosql数据库,支持字符串(string).列表(list).集合(set).散列(hash).有序集合(zset:形如member:score的散列集合,其中member为成员 ...

  3. CentO7 安装 redis, 主从配置,Sentinel集群故障转移切换

        一.Redis的安装(前提是已经安装了EPEL)   安装redis: yum -y install redis 启动/停止/重启 Redis 启动服务: systemctl start re ...

  4. PostgreSQL9.6主从配置

    参考文档: 备机日志传送:https://www.postgresql.org/docs/9.6/static/warm-standby.html 英文文档:https://www.postgresq ...

  5. MySQL主从配置详解

    一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...

  6. Redis学习总结(四)--Redis主从配置

    在分布式系统架构设计中高可用是必须考虑的因素之一.高可用通常是指,通过设计减少系统不能提供服务的时间.而单点是系统高可用的最大的败笔,如果单点出现问题的话,那么整个服务就不能使用了,所以应该尽量在系统 ...

  7. mysql主从配置实现一主一从读写分离

    主从介绍Mysql主从又叫Replication.AB复制.简单讲就是A与B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,实现数据实时同步mysql主从是基于binlog,主上需开启bin ...

  8. mysql主从配置

    引言: 双11,阿里云服务器打折,于是我忍不住又买了一台服务器,于是咱也是有两台服务器的爷们了,既然有了两台服务器,那么肯定要好好利用一下吧,那么就来玩玩mysql的主从配置吧. 准备 两台数据库服务 ...

  9. postgresql pgsql最新版安装指南及数据存储路径更改及主从配置

    postgresql pgsql最新版安装指南及数据存储路径更改及主从配置 安装指南 首先在apt的list添加你当前系统版本对应的apt列表 目前官网有16.04,14.04,12.04 分别对应下 ...

随机推荐

  1. html5中新增的语义化的标签

    html5是html最新的修订版本,2014年10月由万维网联盟(W3C)完成标准制定.目标是取代1999年所制定的HTML 4.01和XHTML 1.0标准,以期能在互联网应用迅速发展的时候,使网络 ...

  2. GridView, ListView 区别

    ListView, GridView部分的类层次结构 AbsListView的xml属性 android:listSelector 当前item高亮时,显示的drawable android:draw ...

  3. 迪米特法則 Law of Demeter

    又稱為"最小知識"原則, 若對Law of Demeter做一個簡單總結: 任何對象的任何方法只能調用以下對象中的方法: (1) 該對象本身 (2) 所傳入的參數對象 (3) 它所 ...

  4. CSS3实现鼠标悬停扩展效果

    我们在做导航标签的时候,有时会出现空间过于拥挤需要隐藏部分内容的情况,所以在这里我自己写了一个鼠标悬停显示扩展内容的效果,如下图所示. 总的来说效果还是比较好实现,但是比较头疼的是三角部分使用了伪元素 ...

  5. c# 使用队列保存图片

    Invoke(new EventHandler(delegate { Bitmap bitmap2 = (Bitmap)eventArgs.Frame.Clone(); ThreadPool.Queu ...

  6. 比较详细的mysql的几种连接功能分析,只要你看完就能学会的好东西

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  7. 转载:.NET Memory Leak: XmlSerializing your way to a Memory Leak

    原文地址:http://blogs.msdn.com/b/tess/archive/2006/02/15/532804.aspx I hate to give away the resolution ...

  8. 继承Application管理生命周期

    继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩 ...

  9. MySQL中在原表中做数据去重(按日期去重,保留id最小的记录)

    表名称 code600300 delete from code600300 where id not in (select minid from (select min(id) as minid fr ...

  10. js正则验证表达式

    //上传文件大小验证 $.fn.checkFileTypeAndSize = function (options) { //默认设置 var defaults = { allowedExtension ...