以下是我收到的错误:

无法创建类型为‘’的‘DbContext’.找不到异常‘方法:’VOID CoreTypeMappingPARAMETERS..ctor(系统类型,Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter,Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer,Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer,Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer,System.Func`3<Microsoft.EntityFrameworkCore.Metadata.IProperty,Microsoft.EntityFrameworkCore.Metadata.IEntityType,Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator>)‘.’在try 创建实例时引发.有关设计时支持的不同模式,请参见https://go.microsoft.com/fwlink/?linkid=851728

连接

"dbcs": "Server=localhost;Port=3307;Database=asp1;user=root;password=;" //this is located in json file
var connectionString = builder.Configuration.Get连接String("dbcs");
builder.Services.AddDbContext<StudentDbContext>(item =>
{
    item.UseMySql(connectionString,ServerVersion.AutoDetect(connectionString));
});
var app = builder.Build();

DbContext:

using Microsoft.EntityFrameworkCore;

namespace WebApplication2.Models
{
    public class StudentDbContext:DbContext
    {
        public StudentDbContext(DbContextOptions op) : base(op)
        {    
        }

        public DbSet<StudentModel> Students { get; set; }
    }
}

型号类别:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication2.Models
{
    public class StudentModel
    {
        [Key]
        public int Id { get; set; }
        [Required(ErrorMessage = "Fill the name")]
        [Column("Student Name", TypeName="varchar(20)")]
        public string Name { get; set; }   
        public int Age { get; set; }

        public string Gender { get; set; }
    }
}

我如何才能解决这个错误?这适用于.NET 8和EF Core 8

推荐答案

I reproduced the error with following package version
enter image description here enter image description here

Microsoft.EntityFrameworkCore(8.0.0)
Microsoft.EntityFrameworkCore.Design(8.0.0)
Microsoft.EntityFrameworkCore.Tools(8.0.0)
Pomelo.EntityFrameworkCore.MySql(7.0.0)

The issue disappeared with follwing package version:
enter image description here enter image description here

Microsoft.EntityFrameworkCore(7.0.14)
Microsoft.EntityFrameworkCore.Design(7.0.14)
Microsoft.EntityFrameworkCore.Tools(7.0.14)
Pomelo.EntityFrameworkCore.MySql(7.0.0)

由于Pomelo.EntityFrameworkCore.Mysql持续支持的版本是7.0.0,因此最好将所有与efcore相关的软件包保留到7.X版本.

Csharp相关问答推荐

在实际上是List T的 IESEARCH上多次调用First()是否不好?

当打印一行x个项目时,如何打印最后一行项目?

. NET 8使用COM向VB6公开

始终保留数组中的最后N个值,丢弃最老的

ASP.NET Core 8.0 JWT验证问题:尽管令牌有效,但SecurityTokenNoExpirationError异常

如何在Parall.ForEachAsync中使用CancerationTokenSource

C#XmlSerializer-输出控制新行的多个XML片段

Quartz调度程序不调用作业(job)类

StackExchange.Redis.RedisServerException:调用ITransaction.ExecuteAsync()时出现错误未知命令取消监视

正在try 从Blazor中的API读取JSON

GODOT 4向C#中的字符串参数发送信号以等待

是否可以将Collectionview中的数组与ObservableCollection绑定?

为什么ReadOnlySpan;T&>没有Slice(...)的重载接受Range实例的?

如何使用EPPlus C#在单个单元格中可视化显示多行文字

如何在绑定到数据库的datagridview中向上或向下移动行

ASP.NET核心中的授权,如何在DbContext启动之前提供租用ID

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果

ASP.NET核心MVC|如何在控制器方法之间传递值

无法创建&Quot;&Quot;类型的实例,因为无法将一个或多个参数绑定到

将ValueTask发送给调用者