skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search