.NET Minimal API无法连接MySQL实例的问题求助
问题场景
搭建了.NET Minimal API项目,已配置MarketDb上下文连接Docker中的MySQL实例,/hello端点可正常返回结果,但访问/regions端点时抛出以下错误:
System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions
object in its constructor and passes it to the base constructor for DbContext.
数据库可通过adminer容器和MySQL浏览器正常访问,上下文配置看起来符合要求,但问题依旧。
核心错误原因
所有实体模型类(TypeId、RegionId、MarketData、CompletedDates)都错误地继承了DbContext。DbContext是EF Core中负责数据库交互的上下文类,实体模型应该是普通的POCO(Plain Old CLR Object)类,不需要继承DbContext。当EF Core尝试加载这些实体时,会将它们当作未配置的DbContext实例处理,从而触发上述错误。
修复方案
移除所有实体类的: DbContext继承,仅保留MarketDb作为项目中唯一的DbContext类。
修正后的实体类代码示例
TypeId.cs
using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; namespace Api.Models { [Table("type_id")] public class TypeId { public int Id { get; set; } public string Value { get; set; } public ICollection<MarketData> MarketData { get; set; } = new List<MarketData>(); public TypeId(int id, string value) { Id = id; Value = value ?? throw new ArgumentNullException(nameof(value)); } } }
RegionId.cs
using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; namespace Api.Models { [Table("region_id")] public class RegionId { public int Id { get; set; } public string Value { get; set; } public ICollection<MarketData> MarketData { get; set; } = new List<MarketData>(); public RegionId(int id, string value) { Id = id; Value = value ?? throw new ArgumentNullException(nameof(value)); } } }
MarketData.cs
using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; namespace Api.Models { [Table("market_data")] [PrimaryKey(nameof(DateID), nameof(RegionID), nameof(TypeID))] public class MarketData { public int DateID { get; set; } public CompletedDates Date { get; set; } public int RegionID { get; set; } public RegionId Region { get; set; } public int TypeID { get; set; } public TypeId Type { get; set; } public double Average { get; set; } public double Highest { get; set; } public double Lowest { get; set; } public long Volume { get; set; } public long OrderCount { get; set; } public MarketData(int dateID, CompletedDates completedDate, int regionID, RegionId region, int typeID, TypeId type, double average, double highest, double lowest, long volume, long orderCount) { DateID = dateID; Date = completedDate ?? throw new ArgumentNullException(nameof(completedDate)); RegionID = regionID; Region = region ?? throw new ArgumentNullException(nameof(region)); TypeID = typeID; Type = type ?? throw new ArgumentNullException(nameof(type)); Average = average; Highest = highest; Lowest = lowest; Volume = volume; OrderCount = orderCount; } } }
CompletedDates.cs
using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; namespace Api.Models { [Table("completed_dates")] public class CompletedDates { public int Id { get; set; } public DateOnly Date { get; set; } public ICollection<MarketData> MarketData { get; set; } = new List<MarketData>(); } }
额外优化建议
为实体类添加无参数构造函数(可设为私有,EF Core可通过反射访问),避免EF Core实例化实体时出现异常:
// 在RegionId类中添加 private RegionId() { }
内容的提问来源于stack exchange,提问作者Catradora

