Entity Framework Core报错‘未为此DbContext配置数据库提供程序’求解决
我是软件工程专业学生,正在学习C#与.NET技术,使用Entity Framework连接本地SQL Server数据库进行数据读写。复刻老师的代码后,始终无法向数据库写入数据,报错‘No Database provider has been configured for this DBContexts’。老师使用OnConfiguring(DbContextOptionsBuilder optionsBuilder)方法配置有效,但我这么做却无效;之后尝试了AddDbContext也未解决问题。当前代码可通过连接字符串创建数据库,但无法通过protected override void OnModelCreating(ModelBuilder modelBuilder)写入种子数据。
附上代码
DbContext类代码
using Microsoft.EntityFrameworkCore; using Opgave1.Classes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Opgave1.DBHandle { public class SkoleKlasseContext : DbContext { public SkoleKlasseContext() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Data Source=DESKTOP-5CSHCTM\\SQLEXPRESS; Initial Catalog = Skoleklasse1; Integrated Security = SSPI; TrustServerCertificate = true"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<SkoleKlasse>().HasData(new SkoleKlasse[] { new SkoleKlasse { KlasseID = -1, KlasseNavn = "21T", Lokale = "A1.23", SpecialKlase = false } }); } public DbSet<SkoleKlasse> skoleKlasser { get; set; } } }
Main类代码
public partial class MainWindow : Window { private SkoleKlasseContext context = new SkoleKlasseContext(); public MainWindow() { InitializeComponent(); // Call EnsureCreated to create the database if it does not exist bool created = context.Database.EnsureCreated(); if (created) { MessageBox.Show("Database created"); } } }
排查与解决方法
1. 修复连接字符串的转义问题
连接字符串中的单反斜杠\会被C#解析为转义字符,导致数据源名称错误,进而无法识别SQL Server提供程序。需要改成双反斜杠\\,或者用@符号让字符串不转义:
// 方式一:双反斜杠 optionsBuilder.UseSqlServer("Data Source=DESKTOP-5CSHCTM\\SQLEXPRESS; Initial Catalog = Skoleklasse1; Integrated Security = SSPI; TrustServerCertificate = true"); // 方式二:@符号 optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-5CSHCTM\SQLEXPRESS; Initial Catalog = Skoleklasse1; Integrated Security = SSPI; TrustServerCertificate = true");
2. 让OnModelCreating中的种子数据生效
EnsureCreated仅负责创建数据库和表结构,不会执行HasData定义的种子数据。要让种子数据写入数据库,需要使用EF的迁移机制:
- 打开Visual Studio的【工具】→【NuGet包管理器】→【包管理器控制台】
- 执行命令创建初始迁移:
Add-Migration InitialCreate - 执行命令将迁移应用到数据库:
Update-Database
如果不想使用迁移,也可以在数据库创建后手动添加数据并保存:
if (created) { context.skoleKlasser.AddRange(new SkoleKlasse[] { new SkoleKlasse { KlasseID = -1, KlasseNavn = "21T", Lokale = "A1.23", SpecialKlase = false } }); context.SaveChanges(); MessageBox.Show("Database created and seed data added"); }
3. 统一DbContext的配置方式
如果尝试过用AddDbContext做依赖注入,要确保配置一致,且避免配置冲突:
- 在Program.cs(或Startup.cs)中配置:
builder.Services.AddDbContext<SkoleKlasseContext>(options => options.UseSqlServer(@"Data Source=DESKTOP-5CSHCTM\SQLEXPRESS; Initial Catalog = Skoleklasse1; Integrated Security = SSPI; TrustServerCertificate = true")); - 同时修改MainWindow,通过构造函数注入DbContext,而不是手动new:
private readonly SkoleKlasseContext _context; public MainWindow(SkoleKlasseContext context) { InitializeComponent(); _context = context; bool created = _context.Database.EnsureCreated(); if (created) { MessageBox.Show("Database created"); } }
依赖注入的配置会覆盖OnConfiguring中的配置,所以如果用依赖注入,要么删除OnConfiguring,要么确保两边连接字符串一致。
4. 完善DbContext构造函数
添加带DbContextOptions<SkoleKlasseContext>参数的构造函数,兼容手动实例化和依赖注入两种场景:
public SkoleKlasseContext(DbContextOptions<SkoleKlasseContext> options) : base(options) { } public SkoleKlasseContext() { }
内容的提问来源于stack exchange,提问作者spill62

