Neigaard
Neigaard

Reputation: 4050

MySQL with Entity Framework on .NET 7

I have the following packages installed:

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?

Upvotes: 4

Views: 6611

Answers (1)

Guru Stron
Guru Stron

Reputation: 141575

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)

UPD

Pomelo package was used.

The following versions of MySqlConnector, EF Core, .NET (Core), .NET Standard and .NET Framework are compatible with published releases of Pomelo.EntityFrameworkCore.MySql:

Release Branch MySqlConnector EF Core .NET (Core) .NET Standard .NET Framework
7.0.0-alpha.1 master >= 2.2.0 7.0.x 6.0+ - -
6.0.2 6.0-maint >= 2.1.2 6.0.x 6.0+ - -
5.0.4 5.0-maint >= 1.3.13 5.0.x 3.0+ 2.1 -
3.2.7 3.2-maint >= 0.69.10 < 1.0.0 3.1.x 2.0+ 2.0 4.6.1+

Upvotes: 6

Related Questions