Nei*_*ard 4 .net c# mysql .net-7.0
我安装了以下软件包:
我有我的上下文文件类:
namespace API.Context
{
public class EventContext : DbContext
{
public EventContext(DbContextOptions<EventContext> options) : base(options)
{
}
public DbSet<Property> Property { get; set; }
public DbSet<Event> Event { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Event>(entity =>
{
entity.HasKey(e => e.ID);
entity.Property(e => e.Title).IsRequired();
});
modelBuilder.Entity<Property>(entity =>
{
entity.HasKey(e => e.ID);
entity.Property(e => e.Key).IsRequired();
entity.Property(e => e.Value).IsRequired();
entity.HasOne(d => d.Event)
.WithMany(p => p.Properties);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的 Program.cs:
using API.Context;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<EventContext>(x => x.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
我的问题是当我运行命令时:
dotnet ef database update
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
System.MissingMethodException:找不到方法:“System.Collections.Generic.IList`1<Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelInitializedConvention> Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet.get_ModelInitializedConventions()”。
我在这里缺少什么?
我安装了以下软件包:
...
- “MySql.EntityFrameworkCore”8.0.22
不,你没有,你有MySql.Data.EntityFrameworkCore,这是一个不支持 .NET 7 的旧包。
对于 .NET 7 支持,您需要安装预览版本MySql.EntityFrameworkCore(最新 - 7.0.0-preview5)或 alpha 版本Pomelo.EntityFrameworkCore.MySql(最新 - 7.0.0-alpha.1)
UPD
使用了柚子包。
以下版本的 MySqlConnector、EF Core、.NET (Core)、.NET Standard 和 .NET Framework 与已发布的版本兼容
Pomelo.EntityFrameworkCore.MySql:
| 发布 | 分支 | MySql连接器 | EF核心 | .NET(核心) | .NET标准 | .NET框架 |
|---|---|---|---|---|---|---|
| 7.0.0-alpha.1 | 掌握 | >= 2.2.0 | 7.0.x | 6.0+ | - | - |
| 6.0.2 | 6.0-维护 | >= 2.1.2 | 6.0.x | 6.0+ | - | - |
| 5.0.4 | 5.0-维护 | >= 1.3.13 | 5.0.x | 3.0+ | 2.1 | - |
| 3.2.7 | 3.2-维护 | >= 0.69.10 < 1.0.0 | 3.1.x | 2.0+ | 2.0 | 4.6.1+ |