efcore 的同步方法
打開 程序包管理器控制臺
cd 項目根目錄(比如 cd C:\Users\Administrator\source\repos\WpfApp2\WpfApp2 回車)
dotnet ef migrations add AddLoginTable
(如果報錯:"No executable found matching command 'dotnet-ef'" 就運行:dotnet tool install --global dotnet-ef)
dotnet ef database update
如果沒有完整的記錄,dotnet ef migrations add AddLoginTable 每次都會生成所有表的更新,所以需要保留
__EFMigrationsHistory表的記錄和項目里自動生成的Migrations文件夾和AppDbContextModelSnapshot.cs,另一個時間名的文件用完后再刪掉)
新增類和修改類都一樣的操作,軟件會自動處理
---------------------------------------------------------------------
主要類:App.xaml.cs
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System.Windows; namespace WpfApp2 { public partial class App : Application { public IServiceProvider ServiceProvider { get; private set; } protected override void OnStartup(StartupEventArgs e) { var services = new ServiceCollection(); // 配置連接字符串 var connectionString = "Server=.\\mssqlserver2016;Database=StudentsData;User Id=sa;Password=123456;TrustServerCertificate=True"; // 注冊 DbContext services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString)); // 注冊其他服務 services.AddTransient<MainWindow>(); ServiceProvider = services.BuildServiceProvider(); //// 創建并顯示主窗口 var mainWindow = ServiceProvider.GetRequiredService<MainWindow>(); mainWindow.Show(); } } }
//Db類
using Microsoft.EntityFrameworkCore; namespace WpfApp2 { public class AppDbContext : DbContext { public static object lockobj = new object(); public DbSet<Login> Logins { get; set; } public DbSet<User> Usera { get; set; } // 構造函數接受 DbContextOptions 并傳遞給基類 public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } } }
窗體:
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public static IServiceScopeFactory serviceScopeFactory = null; public MainWindow() { InitializeComponent(); serviceScopeFactory = ((App)Application.Current).ServiceProvider.GetRequiredService<IServiceScopeFactory>(); Task.Run(() => { while (true) { lock (AppDbContext.lockobj) { using (var scope = serviceScopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); // 子線程專屬實例 var l = dbContext.Logins.ToList(); var temp = l.First(); temp.UserName = "333333"; dbContext.SaveChanges(); } } } }); } } }
工廠類,用于執行命令時報錯用:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; using System.IO; namespace WpfApp2 { // 設計時工廠類,用于 EF Core 工具創建 DbContext public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext> { public AppDbContext CreateDbContext(string[] args) { // 配置連接字符串(與 App.xaml.cs 中的一致) var connectionString = "Server=.\\mssqlserver2016;Database=StudentsData;User Id=sa;Password=123456;TrustServerCertificate=True"; var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>(); optionsBuilder.UseSqlServer(connectionString); return new AppDbContext(optionsBuilder.Options); } } }
浙公網安備 33010602011771號