Пример #1
0
        /// <summary>
        /// 更换数据库, 数据表映射使用传入的映射规则
        /// </summary>
        /// <param name="connStr">新的数据库连接字符串</param>
        /// <param name="rules">映射规则</param>
        /// <exception cref="ArgumentException">type类型不支持</exception>
        public void ChangeDataBase(string connStr, List <TableMappingRule> rules)
        {
            // close
            var accessor = BaseAccessor;

            if (!accessor.IsClose())
            {
                accessor.Close();
            }
            // new base accessor
            accessor = new BaseDataAccessor();
            lock (LockObj)
            {
                var context = accessor.GetDbContext();
                // change connection
                context.Database.GetDbConnection().ConnectionString = connStr;
                foreach (var rule in rules)
                {
                    GetTableName(rule.MappingType, accessor);
                    // get table name
                    string tableNm = rule.Mapper.GetMappingTableName(rule.MappingType, rule.Condition);
                    if (context.Model.FindEntityType(rule.MappingType)?.Relational() is RelationalEntityTypeAnnotations relational)
                    {
                        relational.TableName = tableNm;
                    }
                }
            }
            BaseAccessor = accessor;
        }
Пример #2
0
        /// <summary>
        /// 更换数据库
        /// </summary>
        /// <param name="connectString">新的数据库连接字符串</param>
        public void ChangeDataBase(string connectString)
        {
            // close
            var accessor = BaseAccessor;

            if (!accessor.IsClose())
            {
                accessor.Close();
            }
            // new base accessor
            accessor = new BaseDataAccessor();
            accessor.GetDbContext().Database.GetDbConnection().ConnectionString = connectString;
            BaseAccessor = accessor;
        }
Пример #3
0
        private TableAccessMapping GetTableName(Type mappingType, BaseDataAccessor helper)
        {
            var context = helper.GetDbContext();
            var model   = context.Model.FindEntityType(mappingType);

            if (model != null)
            {
                string table = model.Relational().TableName;
                return(new TableAccessMapping(mappingType, table));
            }
            else
            {
                throw new ArgumentException("Mapping type not found");
            }
        }
Пример #4
0
        /// <summary>
        /// 根据条件改变多个传入类型的映射数据表(此操作会导致当前操作的context释放掉,调用前需确保context的内容已保存)
        /// </summary>
        /// <param name="rules">映射规则</param>
        /// <exception cref="ArgumentException">type类型不支持</exception>
        /// <returns>改变后的数据表映射</returns>
        public List <TableAccessMapping> ChangeMappingTables(List <TableMappingRule> rules)
        {
            List <TableAccessMapping> ret = new List <TableAccessMapping>();

            if (rules != null)
            {
                lock (LockObj)
                {
                    // close
                    var accessor = BaseAccessor;
                    if (!accessor.IsClose())
                    {
                        accessor.Close();
                    }
                    // new base accessor
                    accessor = new BaseDataAccessor();

                    // set new mapping
                    DynamicModelCacheKeyFactory.ChangeTableMapping();

                    var context = accessor.GetDbContext();

                    foreach (var rule in rules)
                    {
                        // check changeable
                        var mapping = GetTableName(rule.MappingType, accessor);

                        // get table name
                        string tableNm = rule.Mapper.GetMappingTableName(rule.MappingType, rule.Condition);
                        if (context.Model.FindEntityType(rule.MappingType)?.Relational() is RelationalEntityTypeAnnotations relational)
                        {
                            relational.TableName = tableNm;
                        }
                        else
                        {
                            tableNm = "";
                        }
                        ret.Add(new TableAccessMapping(rule.MappingType, tableNm));
                    }
                    // set value
                    BaseAccessor = accessor;
                }
            }

            return(ret);
        }
Пример #5
0
        /// <summary>
        /// 获取所有实体类的数据表映射结构体
        /// </summary>
        /// <returns>映射关系集合</returns>
        public List <TableAccessMapping> GetTableNames()
        {
            BaseDataAccessor helper = BaseAccessor;
            var context             = helper.GetDbContext();

            List <TableAccessMapping> ret = new List <TableAccessMapping>();
            var models = context.Model.GetEntityTypes();

            foreach (var model in models)
            {
                string             table   = model.Relational().TableName;
                Type               type    = model.ClrType;
                TableAccessMapping mapping = new TableAccessMapping(type, table);
                ret.Add(mapping);
            }

            return(ret);
        }
Пример #6
0
 /// <summary>
 /// 使用该类前必须调用此方法指明DbContext类型,若传入的类型不是继承于DbContext,将冒ArgumentException
 /// </summary>
 /// <param name="t">Context类型</param>
 /// <exception cref="ArgumentException">context 类型设置错误</exception>
 public static void SetContextType(Type t)
 {
     BaseDataAccessor.SetContextType(t);
 }