示例#1
0
        private SqlSugarClient CreateSqlSugar(ISqlSugarSetting SugarSetting)
        {
            var db = new SqlSugarClient(
                new ConnectionConfig()
            {
                ConnectionString      = SugarSetting.ConnectionString,
                DbType                = SugarSetting.DatabaseType, //设置数据库类型
                IsAutoCloseConnection = true,                      //自动释放数据务,如果存在事务,在事务结束后释放
                InitKeyType           = InitKeyType.Attribute      //从实体特性中读取主键自增列信息
            });

            //用来打印Sql方便你调式
            db.Aop.OnLogExecuting = SugarSetting.LogExecuting;
            return(db);
        }
        public static IServiceCollection AddSqlSugarDbStorage(this IServiceCollection services,
                                                              ISqlSugarSetting defaultDbSetting)
        {
            if (defaultDbSetting == null)
            {
                throw new ArgumentNullException(nameof(defaultDbSetting));
            }

            services.AddSingleton <ISqlSugarProvider>(new SqlSugarProvider(defaultDbSetting));
            services.AddTransient(typeof(ISqlSugarRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddTransient(typeof(IDbRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddSingleton <ISqlSugarProviderStorage, DefaultSqlSugarProviderStorage>();
            services.AddSingleton <IPartitionDbTableFactory, SqlSugarPartitionDbTableFactory>();

            return(services);
        }
示例#3
0
 public SqlSugarProvider(ISqlSugarSetting SugarSetting)
 {
     this.Sugar        = this.CreateSqlSugar(SugarSetting);
     this.ProviderName = SugarSetting.Name;
 }
        public static IServiceProvider AddSqlSugarDatabaseProvider(this IServiceProvider serviceProvider, ISqlSugarSetting dbSetting)
        {
            if (dbSetting == null)
            {
                throw new ArgumentNullException(nameof(dbSetting));
            }

            var fSqlProviderStorage = serviceProvider.GetRequiredService <ISqlSugarProviderStorage>();

            fSqlProviderStorage.AddOrUpdate(dbSetting.Name, new SqlSugarProvider(dbSetting));

            return(serviceProvider);
        }