WPF 使用 DI EF CORE SQLITE

WPF 使用 DI EF CORE SQLITE

1.安装 nuget包

复制代码
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />

2.创建DbContext的实现类,创建有参构造函数

复制代码
  public XXContext(DbContextOptions<XXXContext> options) : base(options)
  {
  }

不创建有参构造函数会注入会报下面的错误

复制代码
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 配置文件中增加配置项

复制代码
{
  "ConnectionStrings": {
    "XXContextSQLite": "Data Source=XX.db"
  }
 }

4.在主机配置中增加配置

复制代码
    private static readonly IHost _host = Host.CreateDefaultBuilder()
        //.ConfigureLogging(config => { config.AddConsole(); })
        .ConfigureServices((context, services) =>
        {
            services.AddSingleton<INavigationService, NavigationService>();
            services.AddSingleton<MainWindow>();
            services.AddSingleton<MainWindowViewModel>();
            services.AddDbContext<XXContext>(options =>
            options.UseSqlite(context.Configuration.GetConnectionString("XXContextSQLite")));
        })
        .Build();

5.生成数据库

主机启动后执行 context.Database.EnsureCreated();

会创建配置文件中配置的数据库

复制代码
 _host.Start();

 using (var scope = _host.Services.CreateScope())
 {
     var services = scope.ServiceProvider;
     var context = services.GetRequiredService<XXContext>();
     context.Database.EnsureCreated();
     // DbInitializer.Initialize(context);
 }

6.种子数据

在DbContext 实现类中重写 OnModelCreating 方法

复制代码
 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     modelBuilder.Entity<Channel>().HasData(

     new Channel { ID = 1, ChanelNum = "D1", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 2, ChanelNum = "D2", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 3, ChanelNum = "D3", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 4, ChanelNum = "D4", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 5, ChanelNum = "D5", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 6, ChanelNum = "D6", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 7, ChanelNum = "D7", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 8, ChanelNum = "D8", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 9, ChanelNum = "D9", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 10, ChanelNum = "D10", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 11, ChanelNum = "D11", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 12, ChanelNum = "D12", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 13, ChanelNum = "D13", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 14, ChanelNum = "D14", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 15, ChanelNum = "D15", ChannelName = String.Empty, CreateTime = DateTime.Now },
     new Channel { ID = 16, ChanelNum = "D16", ChannelName = String.Empty, CreateTime = DateTime.Now });
 }

参考

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

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

相关推荐
诸葛务农1 小时前
人形机器人——电子皮肤技术路线:光学式电子皮肤及MIT基于光导纤维的分布式触觉传感电子皮肤
分布式·机器人·wpf
励志五个月成为嵌入式糕手3 小时前
0820 SQlite与c语言的结合
c语言·oracle·sqlite
界面开发小八哥17 小时前
界面控件DevExpress WPF中文教程:Data Grid - 绑定数据
ui·.net·wpf·界面控件·devexpress·ui开发
界面开发小八哥1 天前
图表组件SciChart WPF再升级:v8.9带来油气井图、新交互与可视化增强
信息可视化·wpf·数据可视化·scichart
创可贴治愈心灵2 天前
WPF中UI线程频繁操作造成卡顿的处理
ui·c#·wpf
阿登林3 天前
初步学习WPF-Prism
学习·wpf
PythonicCC3 天前
Django的生命周期
python·django·sqlite
△曉風殘月〆3 天前
WPF MVVM进阶系列教程(三、使用依赖注入)
wpf·mvvm
此wei浩亦3 天前
WPF中使用 using prism.region 报错
c#·wpf·prism