|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|