Browse Source

修复EFCore设计时BUG

宝臣 王 3 months ago
parent
commit
3b0a8b32a1

+ 16 - 0
UniformMaterialManagementSystem/Data/SqliteContextFactory.cs

@@ -0,0 +1,16 @@
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Design;
+
+namespace UniformMaterialManagementSystem.Data
+{
+    public class SqliteContextFactory : IDesignTimeDbContextFactory<SqliteContext>
+    {
+        public SqliteContext CreateDbContext(string[] args)
+        {
+            var optionsBuilder = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilder.UseSqlite("Data Source=UniformMaterialManagementSystem.db");  // 确保替换为你的实际连接字符串
+
+            return new SqliteContext(optionsBuilder.Options);
+        }
+    }
+}

+ 21 - 21
UniformMaterialManagementSystem/Migrations/SqliteContextModelSnapshot.cs

@@ -64,7 +64,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("Companies");
+                    b.ToTable("Companies", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.Contract", b =>
@@ -121,7 +121,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("PurchaseCompanyGuid");
 
-                    b.ToTable("Contracts");
+                    b.ToTable("Contracts", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.ContractDetail", b =>
@@ -168,7 +168,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("MaterialGuid");
 
-                    b.ToTable("ContractDetails");
+                    b.ToTable("ContractDetails", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.DeliveryReceipt", b =>
@@ -252,7 +252,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("DeliveryReceipts");
+                    b.ToTable("DeliveryReceipts", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.DeliveryReceiptDetail", b =>
@@ -287,7 +287,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("DeliveryReceiptGuid");
 
-                    b.ToTable("DeliveryReceiptDetails");
+                    b.ToTable("DeliveryReceiptDetails", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.FactoryLicense", b =>
@@ -340,7 +340,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("InspectApplyGuid");
 
-                    b.ToTable("FactoryLicenses");
+                    b.ToTable("FactoryLicenses", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectApply", b =>
@@ -433,7 +433,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("MaterialGuid");
 
-                    b.ToTable("InspectApplies");
+                    b.ToTable("InspectApplies", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectApplyContractDetail", b =>
@@ -483,7 +483,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("ApplyGuid");
 
-                    b.ToTable("InspectApplyContractDetails");
+                    b.ToTable("InspectApplyContractDetails", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectApplyDetail", b =>
@@ -519,7 +519,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("ApplyGuid");
 
-                    b.ToTable("InspectApplyDetails");
+                    b.ToTable("InspectApplyDetails", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectionOrganization", b =>
@@ -556,7 +556,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("InspectionOrganizations");
+                    b.ToTable("InspectionOrganizations", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectionReport", b =>
@@ -613,7 +613,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("InspectApplyGuid");
 
-                    b.ToTable("InspectionReports");
+                    b.ToTable("InspectionReports", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.InspectionReportDetail", b =>
@@ -644,7 +644,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("InspectionReportGuid");
 
-                    b.ToTable("InspectionReportDetails");
+                    b.ToTable("InspectionReportDetails", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.Material", b =>
@@ -691,7 +691,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("Materials");
+                    b.ToTable("Materials", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.MenuItem", b =>
@@ -729,7 +729,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("ParentGuid");
 
-                    b.ToTable("MenuItems");
+                    b.ToTable("MenuItems", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.Role", b =>
@@ -753,7 +753,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("Roles");
+                    b.ToTable("Roles", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.RoleMenuItem", b =>
@@ -768,7 +768,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("RoleGuid");
 
-                    b.ToTable("RoleMenuItems");
+                    b.ToTable("RoleMenuItems", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.SampleRegistration", b =>
@@ -840,7 +840,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("InspectApplyGuid");
 
-                    b.ToTable("SampleRegistrations");
+                    b.ToTable("SampleRegistrations", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.SerialNumber", b =>
@@ -875,7 +875,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("SerialNumbers");
+                    b.ToTable("SerialNumbers", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.SupervisionUnit", b =>
@@ -903,7 +903,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasKey("Guid");
 
-                    b.ToTable("SupervisionUnits");
+                    b.ToTable("SupervisionUnits", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.User", b =>
@@ -959,7 +959,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("SupervisionUnitGuid");
 
-                    b.ToTable("Users");
+                    b.ToTable("Users", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.UserRole", b =>
@@ -974,7 +974,7 @@ namespace UniformMaterialManagementSystem.Migrations
 
                     b.HasIndex("UserGuid");
 
-                    b.ToTable("UserRoles");
+                    b.ToTable("UserRoles", (string)null);
                 });
 
             modelBuilder.Entity("UniformMaterialManagementSystem.Entities.Contract", b =>

+ 324 - 188
UniformMaterialManagementSystem/Utils/DataBaseUtil.cs

@@ -1,6 +1,9 @@
 using System.IO;
+using System.Reflection;
 using System.Security.Cryptography;
 
+using DocumentFormat.OpenXml.InkML;
+
 using Microsoft.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore.Infrastructure;
 using Microsoft.EntityFrameworkCore.Metadata;
@@ -9,195 +12,328 @@ using UniformMaterialManagementSystem.Data;
 
 namespace UniformMaterialManagementSystem.Utils
 {
-    public static class DataBaseUtil
-    {
-        private static readonly string DataBasePath = Environment.CurrentDirectory + "\\DataBase";
-        private const string Password = "88384e6a-07e0-47e9-8214-4470a16e78da";
-
-        public static async void ExportTable<T>(string targetConnectionString) where T : class
-        {
-            CreateExportFolder();
+    //public static class DataBaseUtil
+    //{
+    //    private static readonly string DataBasePath = Environment.CurrentDirectory + "\\DataBase";
+    //    private const string Password = "88384e6a-07e0-47e9-8214-4470a16e78da";
 
-            var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
-            optionsBuilderSource.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
+    //    private static readonly SqliteContext _context = new SqliteContext("");
 
-            var targetPath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
+    //    public static async void ExportTable<T>(string targetConnectionString,IEnumerable<T> sourceData) where T : class
+    //    {
+    //        CreateExportFolder();
 
-            var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
-            optionsBuilderTarget.UseSqlite($"Data Source={targetPath}");
-
-            await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
-            await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
-
-            // 读取源数据
-            var sourceData = await sourceContext.Set<T>()
-                .IncludeAll()
-                .ToListAsync();
-
-            // 确保目标数据库创建
-            await targetContext.Database.MigrateAsync();
-
-            // 获取目标 DbSet
-            var targetDbSet = targetContext.Set<T>();
-
-            // 添加到目标数据库
-            targetDbSet.AddRange(sourceData);
-            await targetContext.SaveChangesAsync();
-
-            EncryptFile(targetPath, targetConnectionString);
-        }
-
-        public static async void ImportTable<T>(string sourceConnectionString) where T : class
-        {
-            CreateExportFolder();
-
-            var sourcePath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
-            DecryptFile(sourceConnectionString, sourcePath);
-
-            var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
-            optionsBuilderSource.UseSqlite($"Data Source={sourcePath}");
-
-            var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
-            optionsBuilderTarget.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
-
-            await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
-            await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
-
-            // 确保目标数据库创建
-            await targetContext.Database.MigrateAsync();
-
-            // 读取源数据
-            var sourceData = sourceContext.Set<T>()
-                .IncludeAll()
-                .ToList();
-
-            // 获取目标 DbSet
-            var targetDbSet = targetContext.Set<T>();
-
-            // 如果目标 DbSet 有数据,可以选择清除现有数据或更新数据
-            // targetDbSet.RemoveRange(targetDbSet.ToList());
-            // targetContext.SaveChanges();
-
-            // 添加到目标数据库
-            targetDbSet.AddRange(sourceData);
-            await targetContext.SaveChangesAsync();
-        }
-
-        private static void CreateExportFolder()
-        {
-            if (!Directory.Exists(DataBasePath))
-            {
-                Directory.CreateDirectory(DataBasePath);
-            }
-        }
-
-        public static IQueryable<TEntity> IncludeAll<TEntity>(this DbSet<TEntity> dbSet, int maxDepth = int.MaxValue) where TEntity :class
-        {
-            IQueryable<TEntity> result = dbSet;
-            var context = dbSet.GetService<ICurrentDbContext>().Context;
-            var includePaths = GetIncludePaths<TEntity>(context, maxDepth);
-
-            foreach (var includePath in includePaths)
-            {
-                result = result.Include(includePath);
-            }
-
-            return result;
-        }
-
-        private static IEnumerable<string> GetIncludePaths<T>(DbContext context, int maxDepth = int.MaxValue)
-        {
-            if (maxDepth < 0)
-                throw new ArgumentOutOfRangeException(nameof(maxDepth));
-
-            var entityType = context.Model.FindEntityType(typeof(T));
-            var includedNavigations = new HashSet<INavigation>();
-            var stack = new Stack<IEnumerator<INavigation>>();
-
-            while (true)
-            {
-                var entityNavigations = new List<INavigation>();
-
-                if (stack.Count <= maxDepth)
-                {
-                    if (entityType != null)
-                        foreach (var navigation in entityType.GetNavigations())
-                        {
-                            if (includedNavigations.Add(navigation))
-                                entityNavigations.Add(navigation);
-                        }
-                }
-
-                if (entityNavigations.Count == 0)
-                {
-                    if (stack.Count > 0)
-                        yield return string.Join(".", stack.Reverse().Select(e => e.Current.Name));
-                }
-                else
-                {
-                    foreach (var navigation in entityNavigations)
-                    {
-                        var inverseNavigation = navigation.Inverse;
-                        if (inverseNavigation != null)
-                            includedNavigations.Add(inverseNavigation);
-                    }
-
-                    stack.Push(entityNavigations.GetEnumerator());
-                }
-
-                while (stack.Count > 0 && !stack.Peek().MoveNext())
-                    stack.Pop();
-
-                if (stack.Count == 0)
-                    break;
-
-                entityType = stack.Peek().Current.TargetEntityType;
-            }
-        }
-
-        private static void EncryptFile(string inputFile, string outputFile)
-        {
-            // 生成密钥和初始化向量
-            GenerateAesKeyAndIv(out var key, out var iv);
-
-            using var aesAlg = Aes.Create();
-            aesAlg.Key = key;
-            aesAlg.IV = iv;
-
-            var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
-
-            using var outFileStream = new FileStream(outputFile, FileMode.Create);
-            using var csEncrypt = new CryptoStream(outFileStream, encryptor, CryptoStreamMode.Write);
-            using var inFileStream = new FileStream(inputFile, FileMode.Open);
-
-            inFileStream.CopyTo(csEncrypt);
-        }
-
-        private static void DecryptFile(string inputFile, string outputFile)
-        {
-            // 生成密钥和初始化向量
-            GenerateAesKeyAndIv(out var key, out var iv);
-
-            using var aesAlg = Aes.Create();
-            aesAlg.Key = key;
-            aesAlg.IV = iv;
-
-            var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
-
-            using var fileStream = new FileStream(inputFile, FileMode.Open);
-            using var csDecrypt = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read);
-            using var outFileStream = new FileStream(outputFile, FileMode.Create);
-
-            csDecrypt.CopyTo(outFileStream);
-        }
-
-        private static void GenerateAesKeyAndIv(out byte[] key, out byte[] iv)
-        {
-            using var pdb = new Rfc2898DeriveBytes(Password, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
-            ]);
-
-            key = pdb.GetBytes(32); // 256位密钥
-            iv = pdb.GetBytes(16); // 128位IV
-        }
-    }
+    //        var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
+    //        optionsBuilderSource.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
+
+    //        var targetPath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
+
+    //        var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
+    //        optionsBuilderTarget.UseSqlite($"Data Source={targetPath}");
+
+    //        await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
+    //        await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
+
+    //        // 确保目标数据库创建
+    //        await targetContext.Database.MigrateAsync();
+
+    //        // 获取目标 DbSet
+    //        var targetDbSet = targetContext.Set<T>();
+
+    //        // 添加到目标数据库
+    //        targetDbSet.AddRange(sourceData);
+    //        await targetContext.SaveChangesAsync();
+
+    //        EncryptFile(targetPath, targetConnectionString);
+    //    }
+
+    //    public static async void ImportTable<T>(string sourceConnectionString) where T : class
+    //    {
+    //        CreateExportFolder();
+
+    //        var sourcePath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
+    //        DecryptFile(sourceConnectionString, sourcePath);
+
+    //        var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
+    //        optionsBuilderSource.UseSqlite($"Data Source={sourcePath}");
+
+    //        var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
+    //        optionsBuilderTarget.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
+
+    //        await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
+    //        await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
+
+    //        // 确保目标数据库创建
+    //        await targetContext.Database.MigrateAsync();
+
+    //        // 读取源数据
+    //        var sourceData = sourceContext.Set<T>()
+    //            .IncludeAll()
+    //            .ToList();
+
+    //        // 获取目标 DbSet
+    //        var targetDbSet = targetContext.Set<T>();
+
+    //        // 如果目标 DbSet 有数据,可以选择清除现有数据或更新数据
+    //        //targetDbSet.RemoveRange(targetDbSet.ToList());
+    //        //await targetContext.SaveChangesAsync();
+
+    //        // 添加到目标数据库
+    //        targetDbSet.AddRange(sourceData);
+    //        await targetContext.SaveChangesAsync();
+    //    }
+
+    //    public static async Task ImportData<T>(List<T> entities, Func<T, object> keySelector, bool updateExisting = true) where T : class
+    //    {
+    //        var dbSet = _context.Set<T>();
+    //        var existingEntities = await dbSet.ToListAsync();
+
+    //        foreach (var entity in entities)
+    //        {
+    //            var key = keySelector(entity);
+    //            var existingEntity = existingEntities.FirstOrDefault(e => keySelector(e).Equals(key));
+
+    //            if (existingEntity != null && updateExisting)
+    //            {
+    //                // 更新现有实体
+    //                await UpdateEntityWithRelations(existingEntity, entity);
+    //            }
+    //            else if (existingEntity == null)
+    //            {
+    //                // 添加新实体
+    //                await AddEntityWithRelations(entity);
+    //            }
+    //        }
+
+    //        await _context.SaveChangesAsync();
+    //    }
+
+    //    private static async Task UpdateEntityWithRelations<T>(T existingEntity, T newEntity) where T : class
+    //    {
+    //        _context.Entry(existingEntity).CurrentValues.SetValues(newEntity);
+
+    //        foreach (var property in typeof(T).GetProperties())
+    //        {
+    //            if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
+    //            {
+    //                var existingRelatedEntity = property.GetValue(existingEntity);
+    //                var newRelatedEntity = property.GetValue(newEntity);
+
+    //                if (newRelatedEntity != null)
+    //                {
+    //                    if (existingRelatedEntity == null)
+    //                    {
+    //                        // 如果现有实体没有相关实体,但新实体有,则添加新的相关实体
+    //                        property.SetValue(existingEntity, newRelatedEntity);
+    //                        _context.Entry(newRelatedEntity).State = EntityState.Added;
+    //                    }
+    //                    else
+    //                    {
+    //                        // 如果两者都有相关实体,则递归更新
+    //                        await UpdateEntityWithRelations(existingRelatedEntity, newRelatedEntity);
+    //                    }
+    //                }
+    //            }
+    //            else if (property.PropertyType.IsGenericType &&
+    //                     property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
+    //            {
+    //                // 处理集合导航属性
+    //                await HandleCollectionNavigation(existingEntity, newEntity, property);
+    //            }
+    //        }
+    //    }
+
+    //    private static async Task AddEntityWithRelations<T>(T entity) where T : class
+    //    {
+    //        await _context.Set<T>().AddAsync(entity);
+
+    //        foreach (var property in typeof(T).GetProperties())
+    //        {
+    //            if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
+    //            {
+    //                var relatedEntity = property.GetValue(entity);
+    //                if (relatedEntity != null)
+    //                {
+    //                    await AddEntityWithRelations(relatedEntity);
+    //                }
+    //            }
+    //            else if (property.PropertyType.IsGenericType &&
+    //                     property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
+    //            {
+    //                if (property.GetValue(entity) is not IEnumerable<object> collection) continue;
+
+    //                foreach (var item in collection)
+    //                {
+    //                    await AddEntityWithRelations(item);
+    //                }
+    //            }
+    //        }
+    //    }
+
+    //    private static async Task HandleCollectionNavigation<T>(T existingEntity, T newEntity, PropertyInfo property) where T : class
+    //    {
+    //        var existingCollection = property.GetValue(existingEntity) as IEnumerable<object>;
+    //        var newCollection = property.GetValue(newEntity) as IEnumerable<object>;
+
+    //        if (existingCollection == null || newCollection == null)
+    //            return;
+
+    //        var existingList = existingCollection.ToList();
+    //        var newList = newCollection.ToList();
+
+    //        // 获取集合元素类型
+    //        var elementType = property.PropertyType.GetGenericArguments()[0];
+
+    //        // 假设每个元素都有一个 Id 属性作为键
+    //        var keyProperty = elementType.GetProperty("Id");
+    //        if (keyProperty == null)
+    //            throw new InvalidOperationException($"No Id property found for type {elementType.Name}");
+
+    //        foreach (var newItem in newList)
+    //        {
+    //            var newItemKey = keyProperty.GetValue(newItem);
+    //            var existingItem = existingList.FirstOrDefault(e => keyProperty.GetValue(e).Equals(newItemKey));
+
+    //            if (existingItem != null)
+    //            {
+    //                // 更新现有项
+    //                await UpdateEntityWithRelations(existingItem, newItem);
+    //            }
+    //            else
+    //            {
+    //                // 添加新项
+    //                existingList.Add(newItem);
+    //                await AddEntityWithRelations(newItem);
+    //            }
+    //        }
+
+    //        // 移除不再存在的项
+    //        var itemsToRemove = existingList.Where(e => !newList.Any(n => keyProperty.GetValue(n).Equals(keyProperty.GetValue(e)))).ToList();
+    //        foreach (var item in itemsToRemove)
+    //        {
+    //            existingList.Remove(item);
+    //            _context.Entry(item).State = EntityState.Deleted;
+    //        }
+
+    //        // 更新集合
+    //        property.SetValue(existingEntity, existingList);
+    //    }
+
+    //    private static void CreateExportFolder()
+    //    {
+    //        if (!Directory.Exists(DataBasePath))
+    //        {
+    //            Directory.CreateDirectory(DataBasePath);
+    //        }
+    //    }
+
+    //    public static IQueryable<TEntity> IncludeAll<TEntity>(this DbSet<TEntity> dbSet, int maxDepth = int.MaxValue) where TEntity :class
+    //    {
+    //        IQueryable<TEntity> result = dbSet;
+    //        var context = dbSet.GetService<ICurrentDbContext>().Context;
+    //        var includePaths = GetIncludePaths<TEntity>(context, maxDepth);
+
+    //        foreach (var includePath in includePaths)
+    //        {
+    //            result = result.Include(includePath);
+    //        }
+
+    //        return result;
+    //    }
+
+    //    private static IEnumerable<string> GetIncludePaths<T>(DbContext context, int maxDepth = int.MaxValue)
+    //    {
+    //        if (maxDepth < 0)
+    //            throw new ArgumentOutOfRangeException(nameof(maxDepth));
+
+    //        var entityType = context.Model.FindEntityType(typeof(T));
+    //        var includedNavigations = new HashSet<INavigation>();
+    //        var stack = new Stack<IEnumerator<INavigation>>();
+
+    //        while (true)
+    //        {
+    //            var entityNavigations = new List<INavigation>();
+
+    //            if (stack.Count <= maxDepth)
+    //            {
+    //                if (entityType != null)
+    //                    foreach (var navigation in entityType.GetNavigations())
+    //                    {
+    //                        if (includedNavigations.Add(navigation))
+    //                            entityNavigations.Add(navigation);
+    //                    }
+    //            }
+
+    //            if (entityNavigations.Count == 0)
+    //            {
+    //                if (stack.Count > 0)
+    //                    yield return string.Join(".", stack.Reverse().Select(e => e.Current.Name));
+    //            }
+    //            else
+    //            {
+    //                foreach (var navigation in entityNavigations)
+    //                {
+    //                    var inverseNavigation = navigation.Inverse;
+    //                    if (inverseNavigation != null)
+    //                        includedNavigations.Add(inverseNavigation);
+    //                }
+
+    //                stack.Push(entityNavigations.GetEnumerator());
+    //            }
+
+    //            while (stack.Count > 0 && !stack.Peek().MoveNext())
+    //                stack.Pop();
+
+    //            if (stack.Count == 0)
+    //                break;
+
+    //            entityType = stack.Peek().Current.TargetEntityType;
+    //        }
+    //    }
+
+    //    private static void EncryptFile(string inputFile, string outputFile)
+    //    {
+    //        // 生成密钥和初始化向量
+    //        GenerateAesKeyAndIv(out var key, out var iv);
+
+    //        using var aesAlg = Aes.Create();
+    //        aesAlg.Key = key;
+    //        aesAlg.IV = iv;
+
+    //        var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
+
+    //        using var outFileStream = new FileStream(outputFile, FileMode.Create);
+    //        using var csEncrypt = new CryptoStream(outFileStream, encryptor, CryptoStreamMode.Write);
+    //        using var inFileStream = new FileStream(inputFile, FileMode.Open);
+
+    //        inFileStream.CopyTo(csEncrypt);
+    //    }
+
+    //    private static void DecryptFile(string inputFile, string outputFile)
+    //    {
+    //        // 生成密钥和初始化向量
+    //        GenerateAesKeyAndIv(out var key, out var iv);
+
+    //        using var aesAlg = Aes.Create();
+    //        aesAlg.Key = key;
+    //        aesAlg.IV = iv;
+
+    //        var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
+
+    //        using var fileStream = new FileStream(inputFile, FileMode.Open);
+    //        using var csDecrypt = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read);
+    //        using var outFileStream = new FileStream(outputFile, FileMode.Create);
+
+    //        csDecrypt.CopyTo(outFileStream);
+    //    }
+
+    //    private static void GenerateAesKeyAndIv(out byte[] key, out byte[] iv)
+    //    {
+    //        using var pdb = new Rfc2898DeriveBytes(Password, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
+    //        ]);
+
+    //        key = pdb.GetBytes(32); // 256位密钥
+    //        iv = pdb.GetBytes(16); // 128位IV
+    //    }
+    //}
 }