mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-07 01:36:35 +08:00
配置文件开关,数据库读写分离
This commit is contained in:
@@ -5,37 +5,49 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
using Yi.Framework.Model.Models;
|
||||
|
||||
namespace Yi.Framework.Model
|
||||
{
|
||||
//Add-Migration yi-1
|
||||
//Update-Database yi-1
|
||||
public partial class DataContext : DbContext
|
||||
//Add-Migration yi-1
|
||||
//Update-Database yi-1
|
||||
public partial class DataContext : DbContext
|
||||
{
|
||||
private readonly IOptionsMonitor<MySqlConnOptions> _optionsMonitor;
|
||||
private readonly string _connStr;
|
||||
//private readonly IOptionsMonitor<MySqlConnOptions> _optionsMonitor;
|
||||
private string _connStr;
|
||||
public static string DbSelect = DbConst.Mysql;
|
||||
//public DataContext(IOptionsMonitor<MySqlConnOptions> optionsMonitor)
|
||||
//{
|
||||
// _optionsMonitor = optionsMonitor;
|
||||
// _connStr = _optionsMonitor.CurrentValue.WriteUrl;
|
||||
//}
|
||||
public DbContext ToWriteOrRead(string connstr)
|
||||
{
|
||||
_connStr = connstr;
|
||||
return this;
|
||||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
switch (DbSelect)
|
||||
{
|
||||
case DbConst.Mysql:
|
||||
var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));
|
||||
optionsBuilder.UseMySql(_connStr, serverVersion); break;
|
||||
case DbConst.Sqlite:
|
||||
optionsBuilder.UseSqlite(_connStr); break;
|
||||
case DbConst.Sqlserver:
|
||||
optionsBuilder.UseSqlServer(_connStr);break;
|
||||
case DbConst.Oracle:
|
||||
optionsBuilder.UseOracle(_connStr);break;
|
||||
default:
|
||||
Console.WriteLine("错误!请确保你选择了正确的数据库!");break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DataContext(IOptionsMonitor<MySqlConnOptions> optionsMonitor)
|
||||
{
|
||||
_optionsMonitor = optionsMonitor;
|
||||
_connStr = _optionsMonitor.CurrentValue.Url;
|
||||
}
|
||||
|
||||
public DataContext(string connstr)
|
||||
{
|
||||
_connStr = connstr;
|
||||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
//optionsBuilder.UseSqlite(_connStr);
|
||||
var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));
|
||||
optionsBuilder.UseMySql(_connStr, serverVersion);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Model.ModelFactory;
|
||||
using Yi.Framework.Model.Models;
|
||||
|
||||
namespace Yi.Framework.Model.DbInit
|
||||
{
|
||||
public class DataSeed
|
||||
{
|
||||
public async static Task SeedAsync(DbContext _Db)
|
||||
public async static Task SeedAsync(IDbContextFactory _DbFactory)
|
||||
{
|
||||
var _Db= _DbFactory.ConnWriteOrRead(Common.Enum.WriteAndReadEnum.Write);
|
||||
if (!_Db.Set<user>().Any())
|
||||
{
|
||||
await _Db.Set<user>().AddAsync(new user
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Model.ModelFactory
|
||||
{
|
||||
public static class DbContextExtend
|
||||
{
|
||||
public static DbContext ToWriteOrRead(this DbContext dbContext, string conn)
|
||||
{
|
||||
if (dbContext is DataContext)
|
||||
{
|
||||
|
||||
var context= (DataContext)dbContext; // context 是 EFCoreContext 实例;
|
||||
|
||||
return context.ToWriteOrRead(conn);
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
}
|
||||
100
Yi.Framework/Yi.Framework.Model/ModelFactory/DbContextFactory.cs
Normal file
100
Yi.Framework/Yi.Framework.Model/ModelFactory/DbContextFactory.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
|
||||
namespace Yi.Framework.Model.ModelFactory
|
||||
{
|
||||
public class DbContextFactory : IDbContextFactory
|
||||
{
|
||||
|
||||
private DbContext _Context = null;
|
||||
|
||||
private DbConnOptions _readAndWrite = null;
|
||||
|
||||
public static bool MutiDB_Enabled = false;
|
||||
//private static int _iSeed = 0;//应该long
|
||||
|
||||
/// <summary>
|
||||
///能把链接信息也注入进来
|
||||
///需要IOptionsMonitor
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public DbContextFactory(DbContext context, IOptionsMonitor<DbConnOptions> options)
|
||||
{
|
||||
_readAndWrite = options.CurrentValue;
|
||||
this._Context = context;
|
||||
}
|
||||
public DbContext ConnWriteOrRead(WriteAndReadEnum writeAndRead)
|
||||
{
|
||||
//判断枚举,不同的枚举可以创建不同的Context 或者更换Context链接;
|
||||
if (MutiDB_Enabled)
|
||||
{
|
||||
switch (writeAndRead)
|
||||
{
|
||||
case WriteAndReadEnum.Write:
|
||||
ToWrite();
|
||||
break; //选择链接//更换_Context链接 //选择链接
|
||||
case WriteAndReadEnum.Read:
|
||||
ToRead();
|
||||
break; //选择链接//更换_Context链接
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ToWrite();
|
||||
}
|
||||
|
||||
return _Context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更换成主库连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void ToWrite()
|
||||
{
|
||||
string conn = _readAndWrite.WriteUrl;
|
||||
//_Context.Database.GetDbConnection().;
|
||||
_Context.ToWriteOrRead(conn);
|
||||
}
|
||||
|
||||
|
||||
private static int _iSeed = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 更换成主库连接
|
||||
///
|
||||
/// ///策略---数据库查询的负载均衡
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void ToRead()
|
||||
{
|
||||
string conn = string.Empty;
|
||||
{
|
||||
// //随机
|
||||
//int Count= _readAndWrite.ReadConnectionList.Count;
|
||||
//int index= new Random().Next(0, Count);
|
||||
//conn = _readAndWrite.ReadConnectionList[index];
|
||||
}
|
||||
{
|
||||
//来一个轮询
|
||||
conn = this._readAndWrite.ReadUrl[_iSeed++ % this._readAndWrite.ReadUrl.Count];//轮询;
|
||||
}
|
||||
{
|
||||
///是不是可以直接配置到配置文件里面
|
||||
}
|
||||
_Context.ToWriteOrRead(conn);
|
||||
}
|
||||
|
||||
//public DbContext CreateContext()
|
||||
//{
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.Model.ModelFactory
|
||||
{
|
||||
public interface IDbContextFactory
|
||||
{
|
||||
public DbContext ConnWriteOrRead(WriteAndReadEnum writeAndRead);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user