WPF 使用 DI EF CORE SQLITE

[复制链接]
发表于 2025-9-23 04:03:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
WPF 使用 DI EF CORE SQLITE

1.安装 nuget包
  1. <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />
复制代码
2.创建DbContext的实现类,创建有参构造函数
  1.   public XXContext(DbContextOptions<XXXContext> options) : base(options)
  2.   {
  3.   }
复制代码
不创建有参构造函数会注入会报下面的错误
  1. ArgumentException: 'AddDbContext' was called with configuration, but the context type 'XXContext' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'XXContext' should declare a constructor that accepts a DbContextOptions<XXContext> and must pass it to the base constructor for DbContext.
复制代码
3.appsettings.json 设置文件中增长设置项
  1. {
  2.   "ConnectionStrings": {
  3.     "XXContextSQLite": "Data Source=XX.db"
  4.   }
  5. }
复制代码
4.在主机设置中增长设置
  1.     private static readonly IHost _host = Host.CreateDefaultBuilder()
  2.         //.ConfigureLogging(config => { config.AddConsole(); })
  3.         .ConfigureServices((context, services) =>
  4.         {
  5.             services.AddSingleton<INavigationService, NavigationService>();
  6.             services.AddSingleton<MainWindow>();
  7.             services.AddSingleton<MainWindowViewModel>();
  8.             services.AddDbContext<XXContext>(options =>
  9.             options.UseSqlite(context.Configuration.GetConnectionString("XXContextSQLite")));
  10.         })
  11.         .Build();
复制代码
5.生成数据库
主机启动后执行 context.Database.EnsureCreated();
会创建设置文件中设置的数据库
  1. _host.Start();
  2. using (var scope = _host.Services.CreateScope())
  3. {
  4.      var services = scope.ServiceProvider;
  5.      var context = services.GetRequiredService<XXContext>();
  6.      context.Database.EnsureCreated();
  7.      // DbInitializer.Initialize(context);
  8. }
  9.   
复制代码
6.种子数据
在DbContext 实现类中重写 OnModelCreating 方法
  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3.      modelBuilder.Entity<Channel>().HasData(
  4.      new Channel { ID = 1, ChanelNum = "D1", ChannelName = String.Empty, CreateTime = DateTime.Now },
  5.      new Channel { ID = 2, ChanelNum = "D2", ChannelName = String.Empty, CreateTime = DateTime.Now },
  6.      new Channel { ID = 3, ChanelNum = "D3", ChannelName = String.Empty, CreateTime = DateTime.Now },
  7.      new Channel { ID = 4, ChanelNum = "D4", ChannelName = String.Empty, CreateTime = DateTime.Now },
  8.      new Channel { ID = 5, ChanelNum = "D5", ChannelName = String.Empty, CreateTime = DateTime.Now },
  9.      new Channel { ID = 6, ChanelNum = "D6", ChannelName = String.Empty, CreateTime = DateTime.Now },
  10.      new Channel { ID = 7, ChanelNum = "D7", ChannelName = String.Empty, CreateTime = DateTime.Now },
  11.      new Channel { ID = 8, ChanelNum = "D8", ChannelName = String.Empty, CreateTime = DateTime.Now },
  12.      new Channel { ID = 9, ChanelNum = "D9", ChannelName = String.Empty, CreateTime = DateTime.Now },
  13.      new Channel { ID = 10, ChanelNum = "D10", ChannelName = String.Empty, CreateTime = DateTime.Now },
  14.      new Channel { ID = 11, ChanelNum = "D11", ChannelName = String.Empty, CreateTime = DateTime.Now },
  15.      new Channel { ID = 12, ChanelNum = "D12", ChannelName = String.Empty, CreateTime = DateTime.Now },
  16.      new Channel { ID = 13, ChanelNum = "D13", ChannelName = String.Empty, CreateTime = DateTime.Now },
  17.      new Channel { ID = 14, ChanelNum = "D14", ChannelName = String.Empty, CreateTime = DateTime.Now },
  18.      new Channel { ID = 15, ChanelNum = "D15", ChannelName = String.Empty, CreateTime = DateTime.Now },
  19.      new Channel { ID = 16, ChanelNum = "D16", ChannelName = String.Empty, CreateTime = DateTime.Now });
  20. }
复制代码
参考

ASP.NET Core 中的 Razor Pages 和 Entity Framework Core - 第 1 个教程(共 8 个) | Microsoft Learn
ASP.NET Core 中的 Razor Pages 和 Entity Framework Core - 第 1 个教程(共 8 个) | Microsoft Learn
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表