vs2022+wpf+ef core6+sqlite3
  
  
   
    解决这个问题一般有3种方式:
   
   
   
   (1)提前触发初始化,比如在Startup里,或者在应用启动之后手动触发一次。
   
   (2)使用本次版本新增预编译功能。
  
  
   (3) 在仓储类构造函数中添加,dataContext.Database.EnsureCreated();
  
  
   
    预编译的模型方式
   
  
  
   
    采用 预编译的模型方式,降低第一次加载时间。
   
   
   
   我们知道应用在首次使用DbContext时会花费一定的时间来初始化ef模型,仅创建DbContext实例不会初始化ef模型,只有当执行Add、第一个查询、等操作时才会初始化。如果你的模型很大,有几百几千种实体和对应关系,那么初始化操作时间可能就会比较长。
   
   使用
   
    dotnet ef
   
   命令行工具创建已编译的模型。 在继续之前,请确保已
   
    安装了该工具的最新版本
   
   。
  
  
   可将
   
    dotnet ef
   
   安装为全局工具或本地工具。 大多数开发人员偏向于使用以下命令将
   
    dotnet ef
   
   安装为全局工具:
  
  
  dotnet tool install --global dotnet-ef
  
   在将工具用于特定项目之前,需要将
   
    Microsoft.EntityFrameworkCore.Design
   
   添加到该项目中。
  
  
  dotnet add package Microsoft.EntityFrameworkCore.Design
  
   
   首先,使用dotnet ef dbcontext optimize命令生成预编译的模型(--output-dir 和 --namespace 参数指定输出目录和模型的命名空间)。此命令会返回一段提示信息,提示信息里会告诉你怎么用。
  
  PS C:\dotnet\efdocs\samples\core\Miscellaneous\CompiledModels> dotnet ef dbcontext optimize --output-dir MyCompiledModels --namespace MyCompiledModels
Build started...
Build succeeded.
Successfully generated a compiled model, to use it call 'options.UseModel(MyCompiledModels.BlogsContextModel.Instance)'. Run this command again when the model is modified.
  
   然后,将上述提示的代码配置到到dbcontext上:
  
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseModel(MyCompiledModels.BlogsContextModel.Instance)
        .UseSqlite(@"Data Source=test.db");
  
   最后,预编译的模型也有一些不足:
   
   
   不支持全局查询筛选器。
   
   不支持延迟加载和更改跟踪代理。
   
   不支持自定义 IModelCacheKeyFactory 实现。 但是,可以编译多个模型,并根据需要加载相应的模型。
   
   在模型定义或配置更改时,必须重新生成模型来手动同步模型。
   
   
   由于这些不足,只应在 EF Core 启动时间太慢时使用已编译的模型。 编译小型模型通常不太值得使用已编译的模型。
   
   
   预编译的模型到底提升了多少性能?
   
   微软给出来一个示例代码,包含 449 种实体类型、6390 个属性和 720 种关系。 这是一个中等大小的模型。 使用 BenchmarkDotNet 进行度量,首次查询的平均时间为 1.02 秒。 在相同的硬件上,使用已编译的模型可将这一时间缩短到 117 毫秒。 随着模型大小的增加,会保持类似这样相对稳定的 8 到 10 倍的改进。
  
  
   提前触发初始化
  
  
   提前触发初始化,比如在Startup里,或者在应用启动之后手动触发一次。
  
  
   在仓储类构造函数中添加,dataContext.Database.EnsureCreated();
  
  using CTMvvmDemo.MVVM.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace CTMvvmDemo.Respositoies
    public class StudentRespository : IStudentRespository
        private static readonly string connectString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        private readonly DataContext dataContext;
        //保证_personInfo 永远指向ObservableCollection PersonInfo
        public StudentRespository()
            dataContext = new DataContext(connectString);
            // Creates the database if not exists
          dataContext.Database.EnsureCreated();//会加快第一次加载数据库的速度
        public void Add(Student student)
            dataContext.Students.Add(student);
        public Student? Find(string name)
          List<Student>   students = (from stu in dataContext.Students
            where stu.Name == name
            select stu).ToList();
            return students.Count>0 ?students[0]:null;
        public List<Student> GetAllStudents()
            // Stopwatch stopwatch = new Stopwatch();
            //   dataContext.Students.Load();
            //   dataContext.Students.Local.ToObservableCollection();
            List<Student> students = (from stu in dataContext.Students
                                      select stu).ToList();
            return students;