栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.net5 EFCore 封装读写分离

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net5 EFCore 封装读写分离

.net5 EFCore 封装读写分离

要求:分数据库和主从库,

直接上代码
"DbConnect": {
    "DateBaseType": "sql", //sql,oracle,mysql
    "ReadConnectionString": [
      "Data Source=DESKTOP-23DM9NS;Initial Catalog=ExcellentCode; User Id=tudawei;Password=tudawei"
    ],
    "WriteConnectionString": "Data Source=DESKTOP-23DM9NS;Initial Catalog=ExcellentCode; User Id=tudawei;Password=tudawei"
  },
 public class DbConntection
    {
        /// 
        /// 数据库类型
        /// 
        public DateBaseType DateBaseType { set; get; }

        /// 
        /// 读数据库
        /// 
        public List ReadConnectionString { get; set; }

        /// 
        /// 写数据库
        /// 
        public string WriteConnectionString { get; set; }
    }
 services.Configure(Configuration.GetSection(AppSettingConst.DbConnect));//appsetting映射到实体
            //services.AddTransient();
            services.AddTransient();
  public interface IDateContext
    {
        DbContext CreateEF(ReadOrWrite readOrWrite);
    }
 public  class BaseDateContext: IDateContext
    {
        private readonly IOptions _options = null;
        private  IDbConnection _idbConntection = null;

        public BaseDateContext(IOptions options, IDbConnection idbConntection)
        {
            _options = options;
            _idbConntection=idbConntection;

        }
        protected  string CreateDb(ReadOrWrite readOrWrite)
        {
            string strConnString = string.Empty;
            switch (readOrWrite)
            {
                case Enum.ReadOrWrite.Read:
                    strConnString = Dispatcher(_options.Value.ReadConnectionString);
                    break;
                case Enum.ReadOrWrite.Write:
                    strConnString = _options.Value.WriteConnectionString;
                    break;
                default:
                    break;
            }
            return strConnString;
        }
        public DbContext CreateEF(ReadOrWrite readOrWrite)
        {
        //这一段代码有待优化,不应该出现NEW的情况,可以改成注入
            EFContext db = new EFContext();          
            return db.ToWriteOrRead(CreateDb(readOrWrite), _options);
        }


        /// 
        /// 调度分配--随机就是平均
        /// 
        /// 
        /// 
        private  string Dispatcher(List connectionStrings)
        {
            int _Seed = 0;
            string conn = connectionStrings[_Seed++ % connectionStrings.Count];//轮询--seed需要线程安全
       
            return conn;
        }

      
    }
public class EFContext : DbContext
    {

        public string Conn = null;
        private IOptions _options = null;

        public DbContext ToWriteOrRead(string conn, IOptions options)
        {
            Conn = conn;
            _options = options;
            return this;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder)
        {
            switch (_options.Value.DateBaseType)
            {
                case Enum.DateBaseType.oracle:
                    optionBuilder.UseOracle(Conn);
                    break;
                case Enum.DateBaseType.mysql:
                    break;
                case Enum.DateBaseType.sql:
                    optionBuilder.UseSqlServer(Conn);
                    break;
                default:
                    break;
            }
        }

        public DbSet User { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);
            modelBuilder.AutoMapperApply();
            //modelBuilder.ApplyConfiguration(new UserMap());
            // Interface that all of our Entity maps implement
          


        }



    }

以上可以完成读写分离的操作,代码有待优化,尤其是在

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909533.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号