I have the following packages installed:
- "Microsoft.EntityFrameworkCore" 7.0.0
- "Microsoft.EntityFrameworkCore.Design" 7.0.0
- "MySql.EntityFrameworkCore" 7.0.0-preview5 MySQL8.0.31
I have my context file class:
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);
});
}
}
}
And my 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();
My problem is that when I run the command:
dotnet ef database update
I get this error:
System.MissingMethodException: Method not found: 'System.Collections.Generic.IList`1<Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelInitializedConvention> Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet.get_ModelInitializedConventions()'.
What am I missing here?
CodePudding user response:
I have the following packages installed:
...
- "MySql.EntityFrameworkCore" 8.0.22
No, you don't, you have MySql.Data.EntityFrameworkCore
, which is a legacy package which does not support .NET 7.
For .NET 7 support you need either install preview version of MySql.EntityFrameworkCore
(latest - 7.0.0-preview5
) or alpha version of Pomelo.EntityFrameworkCore.MySql
(latest - 7.0.0-alpha.1
)