我安裝了以下軟體包:
- “Microsoft.EntityFrameworkCore”7.0.0
- “Microsoft.EntityFrameworkCore.Design”7.0.0
- 《MySql.EntityFrameworkCore》7.0.0-preview5 MySQL8.0.31
我有我的背景關系檔案類:
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);
});
}
}
}
還有我的 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();
我的問題是當我運行命令時:
dotnet ef database update
我收到此錯誤:
System.MissingMethodException:找不到方法:'System.Collections.Generic.IList`1<Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelInitializedConvention> Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet.get_ModelInitializedConventions()'。
我在這里錯過了什么?
uj5u.com熱心網友回復:
我安裝了以下軟體包:
...
- “MySql.EntityFrameworkCore”8.0.22
不,你沒有,你有MySql.Data.EntityFrameworkCore,這是一個不支持 .NET 7 的遺留包。
對于 .NET 7 支持,您需要安裝預覽版MySql.EntityFrameworkCore(latest - 7.0.0-preview5) 或 alpha 版Pomelo.EntityFrameworkCore.MySql(latest - 7.0.0-alpha.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535894.html
