SqlSugar-C#版_SeedData(种子数据)

发布时间 2023-04-11 21:19:36作者: ꧁执笔小白꧂
        /// <summary>
        /// 导入种子数据
        /// 注:批量不可用(指定Entity名时功能可用,通过“classNameSpaces”批量导入时功能不可用)
        /// ① DBSeed文件使用json文件保存;
        /// ② 一张表一个DBSeed文件;
        /// ③ 文件名字与表名保持一致;
        /// </summary>
        /// <param name="dbSeedFileDirec">DB种子数据所在的的文件夹(放在程序目录下)</param>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">指定Entity名;不指定时生成指定作用域中/默认作用域(一般指文件夹名)下所有Entity的表的数据</param>
        /// <param name="dllName">指定实体类的包名</param>
        /// <param name="classNameSpaces">指定实体类的包名</param>
        /// <exception cref="NotImplementedException"></exception>
        public void ImportDBSeed<T>(string dbSeedFileDirec, T entity = null, string dllName = "BOZHON.Repository.dll", string[] classNameSpaces = null) where T : class, new()
        {
            classNameSpaces = classNameSpaces == null ? new string[] { "Entity" } : classNameSpaces;

            var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string dbSeedFileDirecPath = path + dbSeedFileDirec + @"/";
            if (!Directory.Exists(dbSeedFileDirecPath))
            {
                throw new Exception("DB数据初始化失败!在程序目录下找不到DB种子数据文件夹!");
            }

            if (entity is null)
            {
                List<Type> entitylist = new List<Type>();
                if (!string.IsNullOrWhiteSpace(dllName))
                {
                    dllName = path + dllName;
                    Assembly assembly = Assembly.LoadFrom(dllName);
                    Type[] ts = assembly.GetTypes();
                    foreach (string classNameSpace in classNameSpaces)
                    {
                        foreach (Type t in ts)
                        {
                            if (t.FullName.Contains(classNameSpace))
                            {
                                entitylist.Add(t);
                            }
                        }
                    }
                }

                foreach (Type type in entitylist)
                {
                    string dbSeedFilePath = dbSeedFileDirecPath + type.Name + ".json";

                    if (File.Exists(dbSeedFilePath))
                    {
                        Type typeList = typeof(List<>);
                        Type actualType = typeList.MakeGenericType(type);
                        dynamic obj = Activator.CreateInstance(actualType);

                        obj = JsonFileHelper.ReadjsonT<object>(dbSeedFilePath);  // 加载数据
                        //Db.Insertable(obj).ExecuteCommand();  // 未找到合适的无实体插入方法

                        throw new Exception("批量插入请使用方法ImportDBSeed2!");
                    }
                }
            }
            else
            {
                string dbSeedFilePath = dbSeedFileDirecPath + entity.GetType().Name + ".json";
                if (File.Exists(dbSeedFilePath))
                {
                    T obj = JsonFileHelper.ReadjsonT<T>(dbSeedFilePath);  // 加载数据

                    Db.Insertable(obj);
                }
            }
        }

        /// <summary>
        /// 导入种子数据-批量
        /// ① DBSeed文件使用json文件保存;
        /// ② 一张表一个DBSeed文件;
        /// ③ 文件名字与表名保持一致;
        /// </summary>
        /// <param name="dbSeedFileDirec">DB种子数据所在的的文件夹(放在程序目录下)</param>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">指定Entity名;不指定时生成指定作用域中/默认作用域(一般指文件夹名)下所有Entity的表的数据</param>
        /// <param name="dllName">指定实体类的包名</param>
        /// <param name="classNameSpaces">指定实体类的包名</param>
        /// <exception cref="NotImplementedException"></exception>
        public void ImportDBSeed2(string dbSeedFileDirec) 
        {
            var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string dbSeedFileDirecPath = path + dbSeedFileDirec + @"/";
            if (!Directory.Exists(dbSeedFileDirecPath))
            {
                throw new Exception("DB数据初始化失败!在程序目录下找不到DB种子数据文件夹!");
            }

            #region 设置DBSeed
            ImportDBSeed2<TestTable1>(dbSeedFileDirecPath);
            ImportDBSeed2<TestTable2>(dbSeedFileDirecPath);
            ImportDBSeed2<TestTable3>(dbSeedFileDirecPath);
            #endregion 设置DBSeed
        }

        /// <summary>
        /// ImportDBSeed2-导入种子数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbSeedFileDirecPath">文件夹路径</param>
        private void ImportDBSeed2<T>(string dbSeedFileDirecPath) where T : class, new()
        {
            string dbSeedFilePath = dbSeedFileDirecPath + new T().GetType().Name + ".json";
            if (File.Exists(dbSeedFilePath))
            {
                var objs = JsonFileHelper.ReadjsonT<List<T>>(dbSeedFilePath);  // 加载数据

                Db.Insertable<T>(objs).ExecuteCommand();
            }
        }

        /// <summary>
        /// 导出种子数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbSeedFileDirec">DB种子数据导出的文件夹(生成在程序目录下)</param>
        /// <param name="entity">指定Entity名;不指定时生成指定作用域中/默认作用域(一般指文件夹名)下所有Entity的表的数据</param>
        /// <param name="dllName">指定实体类的包名</param>
        /// <param name="classNameSpaces">指定实体类的包名</param>
        public void ExportDBSeed<T>(string dbSeedFileDirec, T entity = null, string dllName = "BOZHON.Repository.dll", string[] classNameSpaces = null) where T : class, new()
        {
            classNameSpaces = classNameSpaces == null ? new string[] { "Entity" } : classNameSpaces;

            var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string dbSeedFileDirecPath = path + dbSeedFileDirec + @"/";
            if (!Directory.Exists(dbSeedFileDirecPath))
            {
                Directory.CreateDirectory(dbSeedFileDirecPath);  // 生成目录
            }

            if (entity is null)
            {
                List<Type> entitylist = new List<Type>();
                if (!string.IsNullOrWhiteSpace(dllName))
                {
                    dllName = path + dllName;
                    Assembly assembly = Assembly.LoadFrom(dllName);
                    Type[] ts = assembly.GetTypes();
                    foreach (string classNameSpace in classNameSpaces)
                    {
                        foreach (Type t in ts)
                        {
                            if (t.FullName.Contains(classNameSpace))
                            {
                                entitylist.Add(t);
                            }
                        }
                    }
                }

                foreach (Type type in entitylist)
                {
                    string dbSeedFilePath = dbSeedFileDirecPath + type.Name + ".json";

                    var seedDatas = Db.Queryable(type.Name, type.Name).ToList();

                    JsonFileHelper.WritejsonT(dbSeedFilePath, seedDatas);
                }
            }
            else
            {
                string dbSeedFilePath = dbSeedFileDirecPath + entity.GetType().Name + ".json";
                var seedDatas = Db.Queryable<T>().ToList();

                JsonFileHelper.WritejsonT(dbSeedFilePath, seedDatas);
            }
        }

详细见:SqlSugar-C#版_安装与使用教程