Giter Club home page Giter Club logo

Comments (4)

shanvenleo avatar shanvenleo commented on May 21, 2024 5

根据 SqlSugar 官方文档简单修改

  1. 添加 BaseDbContext 基类

  2. 添加 DbSet 数据集类

  3. 添加 SugarDbOptions 配置类

  4. 根据项目需要,添加 DbContext01,DbContext02,...,继承于 BaseDbContext

  5. 添加扩展方法 ServiceCollectionExtensions.AddSugarDbContext

  6. 在 ConfigureServices 添加 DbContext 服务注入 ConfigureServices

    具体代码如下

    // 1. 添加 BaseDbContext 基类
	namespace DbContext
{
    public class BaseDbContext
    {
        private readonly ILogger<BaseDbContext> _logger;

        public BaseDbContext(IOptionsSnapshot<SugarDbOptions> namedOptionsAccessor,
            ILoggerFactory loggerFactory
            )
        {
            _logger = loggerFactory.CreateLogger<BaseDbContext>();
            var options = namedOptionsAccessor.Get(this.GetType().Name);
            string connectionString = options.ConnectionString;
            string dbType = options.DbType;
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = (DbType)Enum.Parse(typeof(DbType), dbType),//DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                _logger.LogInformation(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));

            };
        }

        public BaseDbContext(string connectionString, string dbType)
        {
            this.GetType();
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = (DbType)Enum.Parse(typeof(DbType), dbType),//DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                _logger.LogInformation(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };

        }
        //注意:不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
    }
}
// 2. 添加 DbSet 数据集类
namespace DbContext
{
    //可以直接用SimpleClient也可以扩展一个自个的类 
    //推荐直接用 SimpleClient 
    //为了照顾需要扩展的朋友,我们就来扩展一个SimpleClient,取名叫DbSet
    public class DbSet<T> : SimpleClient<T> where T : class, new()
    {
        public DbSet(SqlSugarClient context) : base(context)
        {

        }
        //SimpleClient中的方法满足不了你,你可以扩展自已的方法
        public List<T> GetByIds(dynamic[] ids)
        {
            return Context.Queryable<T>().In(ids).ToList(); ;
        }
    }
}
// 3. 添加 SugarDbOptions 配置类
namespace DbContext
{
    public class SugarDbOptions
    {
        public string ConnectionString { get; set; } //连接字符串
        public string DbType { get; set; } //数据库类型
    }
}
// 4. 根据项目需要,添加 DbContext01,DbContext02,...,继承于 BaseDbContext 
namespace DbContext
{
    public class DbContext01 : BaseDbContext
    {
        public DbContext01(IOptionsSnapshot<SugarDbOptions> namedOptionsAccessor,ILoggerFactory loggerFactory):base(namedOptionsAccessor, loggerFactory){}
        // DbSet数据表
        public DbSet<1>1 { get { return new DbSet<1>(Db); } }
        public DbSet<2>2 { get { return new DbSet<2>(Db); } }
        public DbSet<3>3 { get { return new DbSet<3>(Db); } }
    }
}
// 5. 添加扩展方法 ServiceCollectionExtensions.AddSugarDbContext
namespace Extensions
{
        public static IServiceCollection AddSugarDbContext<T>(this IServiceCollection services, Action<SugarDbOptions> setupAction) where T : class
        {
            services.AddScoped<T>();
            string name = typeof(T).Name;
            var options = services.AddOptions<SugarDbOptions>(name);
            services.Configure<SugarDbOptions>(name, setupAction);
            return services;
        }
    }
}
// 6. 在 ConfigureServices 添加 DbContext 服务注入 ConfigureServices
            services.AddSugarDbContext<DbContext01>(options =>
            {
                options.ConnectionString = Configuration["DbContext:DbContext01:ConnectionString"];
                options.DbType = Configuration["DbContext:DbContext01:DbType"];
            });
//对应配置文件
  "DbContext": {
    "DbContext01": {
      "Enabled": true,
      "ConnectionString": "Server=.;Database=Db01;User ID=sa;Password=123;",
      "DbType": "SqlServer"
    }
}
  1. 后续问题
    这个方法能解决按不同业务模块分库的问题.对于水平分库扩展还有改进的空间.
    比如项目初期10万用户以内只有一个库,每增加10万用户自动增加一个库.
    或者多租户系统,每个增加租户添加一个库.
    需要根据不同的库动态切换连接字符串.
    暂时还没有想到解决方案....

from blog.core.

anjoy8 avatar anjoy8 commented on May 21, 2024

你这个是配置多个上下文,每个上下文对应一个db库,如果使用的话,再去切换吧。

他问是好像是同时操作两个db,就是你说的,A租户过来自动使用A数据库连接字符串,B操作B。

from blog.core.

binyly avatar binyly commented on May 21, 2024

services.AddScoped(provider =>
{
Func<string, SqlSugarClient> func = key =>
{
switch (key)
{
case "1":
return new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = sqlSugarConfig.Item2,
DbType = sqlSugarConfig.Item1,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
});
case "2":
return new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = sqlSugarConfig.Item2,
DbType = sqlSugarConfig.Item1,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
});

                    default:
                        throw new NotSupportedException($"Not Support key : {key}");
                }
            };
            return func;
        });

private readonly Func<string, SqlSugarClient> _serviceAccessor;
public HomeController( Func<string, SqlSugarClient> serviceAccessor
)
{
_serviceAccessor=serviceAccessor;
}
获取方式 var db =_serviceAccessor("1")

这是另外一个大佬提供的方案,可以学习一下

from blog.core.

anjoy8 avatar anjoy8 commented on May 21, 2024

我这边实现了多库操作,但是不是多个上下文的。
#46

from blog.core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.