Переглянути джерело

添加数据包导入导出功能

宝臣 王 3 місяців тому
батько
коміт
22441d8e04

+ 5 - 0
UniformMaterialManagementSystem/Data/SqliteContext.cs

@@ -1,8 +1,10 @@
 using System.Diagnostics;
+
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Logging;
 
 using UniformMaterialManagementSystem.Entities;
+
 using Contract = UniformMaterialManagementSystem.Entities.Contract;
 using Material = UniformMaterialManagementSystem.Entities.Material;
 
@@ -52,6 +54,9 @@ namespace UniformMaterialManagementSystem.Data
 
         public DbSet<FactoryLicense> FactoryLicenses { get; set; }
 
+        public SqliteContext() { }
+
+        public SqliteContext(DbContextOptions<SqliteContext> options) : base(options) { }
 
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {

+ 63 - 0
UniformMaterialManagementSystem/Utils/DataBaseUtil.cs

@@ -0,0 +1,63 @@
+using Microsoft.EntityFrameworkCore;
+
+using UniformMaterialManagementSystem.Data;
+
+namespace UniformMaterialManagementSystem.Utils
+{
+    public static class DataBaseUtil
+    {
+        public static void ExportTable<T>(string targetConnectionString) where T : class
+        {
+            var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilderSource.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
+
+            var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilderTarget.UseSqlite(targetConnectionString);
+
+            using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
+            using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
+
+            // 读取源数据
+            var sourceData = sourceContext.Set<T>().ToList();
+
+            // 确保目标数据库创建
+            targetContext.Database.EnsureCreated();
+
+            // 获取目标 DbSet
+            var targetDbSet = targetContext.Set<T>();
+
+            // 添加到目标数据库
+            targetDbSet.AddRange(sourceData);
+            targetContext.SaveChanges();
+        }
+
+        public static void ImportTable<T>(string sourceConnectionString) where T : class
+        {
+            var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilderSource.UseSqlite(sourceConnectionString);
+
+            var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
+            optionsBuilderTarget.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
+
+            using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
+            using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
+
+            // 确保目标数据库创建
+            targetContext.Database.EnsureCreated();
+
+            // 读取源数据
+            var sourceData = sourceContext.Set<T>().ToList();
+
+            // 获取目标 DbSet
+            var targetDbSet = targetContext.Set<T>();
+
+            // 如果目标 DbSet 有数据,可以选择清除现有数据或更新数据
+            // targetDbSet.RemoveRange(targetDbSet.ToList());
+            // targetContext.SaveChanges();
+
+            // 添加到目标数据库
+            targetDbSet.AddRange(sourceData);
+            targetContext.SaveChanges();
+        }
+    }
+}