/// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="conString">数据库连接名或连接字符串</param>
 /// <param name="dbType">数据库类型</param>
 /// <param name="entityNamespace">数据库实体命名空间,注意,该命名空间应该包含所有需要的数据库实体</param>
 public RepositoryDbContext(string conString, DatabaseType dbType)
 {
     _conString = conString;
     _dbType    = dbType;
     RefreshDb();
     DbModelFactory.AddObserver(this);
 }
Exemple #2
0
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="conString"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        internal static BaseDbContext GetDbContext([NotNull] string conString, DatabaseType dbType)
        {
            if (conString.IsNullOrEmpty())
            {
                throw new Exception("conString能为空");
            }
            var dbConnection = DbProviderFactoryHelper.GetDbConnection(conString, dbType);
            var model        = DbModelFactory.GetDbCompiledModel(conString, dbType);
            DbContextOptionsBuilder builder = new DbContextOptionsBuilder();

            switch (dbType)
            {
            case DatabaseType.SqlServer: builder.UseSqlServer(dbConnection, x => x.UseRowNumberForPaging()); break;

            case DatabaseType.MySql: builder.UseMySql(dbConnection); break;

            case DatabaseType.PostgreSql: builder.UseNpgsql(dbConnection); break;

            case DatabaseType.Oracle: builder.UseOracle(dbConnection, x => x.UseOracleSQLCompatibility("11")); break;

            default: throw new Exception("暂不支持该数据库!");
            }
            builder.EnableSensitiveDataLogging();
            builder.UseModel(model);
            builder.UseLoggerFactory(_loggerFactory);

            return(new BaseDbContext(builder.Options));
        }
Exemple #3
0
        private Type CheckModel(Type type)
        {
            (bool needRefresh, Type model)model = DbModelFactory.GetModel(type);
            if (model.needRefresh)
            {
                RefreshDb();
            }

            return(model.model);
        }
Exemple #4
0
        private void RefreshDb()
        {
            var oldDb           = _db;
            var con             = DbProviderFactoryHelper.GetDbConnection(_conString, _dbType);
            var dBCompiledModel = DbModelFactory.GetDbCompiledModel(_conString, _dbType);

            _db = new BaseDbContext(con, dBCompiledModel);
            if (oldDb != null)
            {
                _db.Database.Log += oldDb.Database.Log;
            }
        }
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             _db?.Dispose();
         }
         _transaction = null;
         DbModelFactory.RemoveObserver(this);
         disposedValue = true;
     }
 }
        public void RefreshDb()
        {
            //重用DbConnection,使用底层相同的DbConnection,支持Model持热更新
            DbConnection con = null;

            if (_transaction != null)
            {
                con = _transaction.Connection;
            }
            else
            {
                con = _db?.Database?.GetDbConnection() ?? DbProviderFactoryHelper.GetDbConnection(_conString, _dbType);
            }

            var dBCompiledModel = DbModelFactory.GetDbCompiledModel(_conString, _dbType);

            _db = new BaseDbContext(_dbType, con, dBCompiledModel);
            _db.Database.UseTransaction(_transaction);
            disposedValue = false;
        }
        void IAddAbstractTable.AddAbsTable(string absTableName, Action <IAddPhysicTable> physicTableBuilder, Func <object, string> findTable)
        {
            IAddPhysicTable physicBuilder = new ShardingConfigBootstrapper();

            physicTableBuilder(physicBuilder);
            var value = physicBuilder.GetPropertyValue("_physicTables") as List <(string physicTableName, string dataSourceName)>;

            _absTables.Add(new AbstractTable
            {
                AbsTableName = absTableName,
                FindTable    = findTable,
                PhysicTables = value
            });

            //动态实体模型
            var absEntityType = DbModelFactory.GetEntityType(absTableName);

            value.ForEach(aPhysicTable =>
            {
                DbModelFactory.AddEntityType(aPhysicTable.physicTableName, ShardingHelper.MapTable(absEntityType, aPhysicTable.physicTableName));
            });
        }
        private Type CheckModel(Type type)
        {
            Type model = DbModelFactory.GetModel(type);

            return(model);
        }
Exemple #9
0
 private Type MapTable(string targetTableName)
 {
     return(DbModelFactory.GetEntityType(targetTableName));
 }