以下是我收到的错误:
无法创建类型为‘’的‘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