ソースを参照

添加获取数据方法

宝臣 王 3 ヶ月 前
コミット
f32a078c75

+ 21 - 5
UniformMaterialManagementSystem/Utils/DataBaseUtil.cs

@@ -193,6 +193,22 @@ namespace UniformMaterialManagementSystem.Utils
             connection.Close();
         }
 
+        public static async Task<IEnumerable<T>> QueryAll<T>(string path, Func<IQueryable<T>, IQueryable<T>>? includeFunc = null) where T : class
+        {
+            var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilderSource.UseSqlite($"Data Source={path}");
+
+            await using var targetContext = new SqliteContext(optionsBuilderSource.Options);
+
+            var query = targetContext.Set<T>().AsQueryable();
+            if (includeFunc != null)
+            {
+                query = includeFunc(query);
+            }
+
+            return query.ToList();
+        }
+
         private static void CreateExportFolder()
         {
             if (!Directory.Exists(DataBasePath))
@@ -262,7 +278,7 @@ namespace UniformMaterialManagementSystem.Utils
             iv = pdb.GetBytes(16); // 128位IV
         }
 
-        public static (string sql, List<SqliteParameter> parameters) ExistsSql<T>(T entity, string tableName) where T : class?
+        private static (string sql, List<SqliteParameter> parameters) ExistsSql<T>(T entity, string tableName) where T : class?
         {
             var type = typeof(T);
             var primaryKeyProps = GetPrimaryKeyProperties<T>().ToList();
@@ -286,7 +302,7 @@ namespace UniformMaterialManagementSystem.Utils
                 .Where(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Any());
         }
 
-        public static (string sql, List<SqliteParameter> parameters) InsertSql<T>(T entity, string tableName) where T : class?
+        private static (string sql, List<SqliteParameter> parameters) InsertSql<T>(T entity, string tableName) where T : class?
         {
             var type = typeof(T);
             var properties = type.GetProperties().Where(p => !IsNavigationProperty(p)).ToArray();
@@ -300,7 +316,7 @@ namespace UniformMaterialManagementSystem.Utils
             return (sql, parameters);
         }
 
-        public static (string sql, List<SqliteParameter> parameters) UpdateSql<T>(T entity, string tableName) where T : class?
+        private static (string sql, List<SqliteParameter> parameters) UpdateSql<T>(T entity, string tableName) where T : class?
         {
             var type = typeof(T);
             var primaryKeyProps = GetPrimaryKeyProperties<T>().ToList();
@@ -315,7 +331,7 @@ namespace UniformMaterialManagementSystem.Utils
             return (sql, parameters);
         }
 
-        public static (string sql, List<SqliteParameter> parameters) DeleteSql<T>(T entity, string tableName) where T : class?
+        private static (string sql, List<SqliteParameter> parameters) DeleteSql<T>(T entity, string tableName) where T : class?
         {
             var primaryKeyProps = GetPrimaryKeyProperties<T>();
             var whereClause = string.Join(" AND ", primaryKeyProps.Select(p => $"{p.Name} = @{p.Name}"));
@@ -326,7 +342,7 @@ namespace UniformMaterialManagementSystem.Utils
             return (sql, parameters);
         }
 
-        public static List<SqliteParameter> CreateParameters<T>(T entity, bool includeAllProperties = true) where T : class?
+        private static List<SqliteParameter> CreateParameters<T>(T entity, bool includeAllProperties = true) where T : class?
         {
             var parameters = new List<SqliteParameter>();
             var properties = typeof(T).GetProperties().Where(p => !IsNavigationProperty(p));

+ 6 - 1
UniformMaterialManagementSystem/ViewModels/LoginPageViewModel.cs

@@ -83,7 +83,12 @@ namespace UniformMaterialManagementSystem.ViewModels
 
             //DataBaseUtil.ExportData();
 
-            DataBaseUtil.ImportData();
+            //var path = DataBaseUtil.ImportData();
+            //_ = DataBaseUtil.QueryAll<User>(path, q =>
+            //                q.Include(x => x.Company)
+            //                    .Include(x => x.SupervisionUnit)
+            //                    .Include(x => x.Roles)
+            //                    .Include(x => x.UserRoles));
 
             // 验证所有属性
             ValidateAllProperties();