如何快速实现数据库读写分离

使AOP

```
using  System;
using  System.Linq;
using  System.Transactions;
using  System.Collections.Generic;
using  Microsoft.EntityFrameworkCore;
using  Microsoft.EntityFrameworkCore.Diagnostics;

namespace  DataAccessLayer
{
        public  class  MainDbContext  :  DbContext
        {
                private  readonly  string  _readConnectionString;
                private  readonly  string  _writeConnectionString;
                private  readonly  List<string>  _readEntityNames;
 
                public  MainDbContext(
                        string  readConnectionString,
                        string  writeConnectionString,
                        List<string>  readEntityNames)
                {
                        _readConnectionString  =  readConnectionString;
                        _writeConnectionString  =  writeConnectionString;
                        _readEntityNames  =  readEntityNames;
                }
 
                protected  override  void  OnConfiguring(DbContextOptionsBuilder  optionsBuilder)
                {
                        optionsBuilder.ConfigureWarnings(
                                warnings  =>  warnings.Throw(CoreEventId.FirstWithoutOrderByAndFilterWarning));
 
                        //  
                        optionsBuilder.UseSqlServer(_writeConnectionString)
                                .EnableSensitiveDataLogging();
                        base.OnConfiguring(optionsBuilder);
                }
 
                protected  override  void  OnModelCreating(ModelBuilder  modelBuilder)
                {
                        //  
                        modelBuilder.Model.SetReadOnly(false);
                        base.OnModelCreating(modelBuilder);
                }
 
                public  override  int  SaveChanges()
                {
                        //  
                        using  (var  transactionScope  =  new  TransactionScope(TransactionScopeOption.Required,
                                new  TransactionOptions  {  IsolationLevel  =  IsolationLevel.ReadUncommitted,  Timeout  =  TimeSpan.FromSeconds(30)  }))
                        {
                                //  
                                var  writeEntities  =  ChangeTracker.Entries()
                                        .Where(e  =>  e.State  ==  EntityState.Added  ||  e.State  ==  EntityState.Modified  ||  e.State  ==  EntityState.Deleted)
                                        .Select(e  =>  e.Entity.GetType().Name).Distinct().Except(_readEntityNames)
                                        .ToList();
                                var  readEntities  =  ChangeTracker.Entries()
                                        .Where(e  =>  e.State  ==  EntityState.Unchanged)
                                        .Select(e  =>  e.Entity.GetType().Name).Distinct().Intersect(_readEntityNames)
                                        .ToList();
 
                                //  
                                bool  isRead  =  false;
                                var  connection  =  this.Database.GetDbConnection();
                                connection.Open();
 
                                foreach  (var  entity  in  writeEntities)
                                {
                                        this.Database.UseTransaction(null);
                                        isRead  =  false;
                                }
 
                                foreach  (var  entity  in  readEntities)
                                {
                                        this.Database.UseTransaction(Transaction.Current);
                                        isRead  =  true;
                                }
 
                                //
                                var  result  =  base.SaveChanges();
                                transactionScope.Complete();
                                return  result;
                        }
                }
        }
}
```

MainDbContextDbContextOnConfiguring()UseSqlServer()SaveChanges()AOP

posted @ 2023-03-25 19:41  yswenli  阅读(164)  评论(0编辑  收藏  举报