Riley-Howley
Riley-Howley

Reputation: 69

asp.net 6 EF using MySql Database not working

I am trying to connect my Web API build in asp.net 6 with MySql and I am having a lot of trouble

My Service looks like this

builder.Services.AddDbContext<TicketApiContext>(options =>
    options.UseMySQL("CONNECTION_STRING"));

and my Database Context like this.

using Microsoft.EntityFrameworkCore;
namespace VelocityNetAPI.Data
{
    public class TicketApiContext : DbContext
    {
        public TicketApiContext(DbContextOptions<TicketApiContext> options)
            : base(options)
        {
        }
        public DbSet<VelocityNetAPI.Models.Client> Client { get; set; }

        public DbSet<VelocityNetAPI.Models.Job> Job { get; set; }

        public DbSet<VelocityNetAPI.Models.User> User { get; set; }

        public DbSet<VelocityNetAPI.Models.Dev> Dev { get; set; }

        public DbSet<VelocityNetAPI.Models.FinishedJobs> FinishedJobs { get; set; }
        

    }
}

when I run add-migration initial I get an error

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies' while attempting to activate 'MySql.EntityFrameworkCore.Storage.Internal.MySQLTypeMappingSource'.

Any help will be very much appreciated.Here is my Nuget packages

Please help I am completely lost

Cheers All

Upvotes: 1

Views: 2272

Answers (2)

Adam Croot
Adam Croot

Reputation: 108

I did an investigation into this here: https://bugs.mysql.com/bug.php?id=106592

public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEntityFrameworkMySQL();
        new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
            .TryAddCoreServices();
    }
}

Should be a workaround to it. It seems that in .NET6, TryAddCoreServices() was introduced but not implemented in Mysql.EntityFrameworkCore

I've not heard much on if anybody has had any issue with this, but it worked for me.

Upvotes: 2

Riley-Howley
Riley-Howley

Reputation: 69

So after tweaking I have the solution. Using the pomelo package and this line.

builder.Services.AddDbContext<TicketAPIContext>(options =>
  options.UseMySql(builder.Configuration.GetConnectionString("TicketApiContextMySql"), new MySqlServerVersion(new Version(8, 0, 22))));

Hope this helps devs

Upvotes: 2

Related Questions