1

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

4
  • Are all the dependencies installed too? You don't need SQLServer package, unless you're targeting both dbs. Have you tried using v5 instead of v6? e.g. questions like stackoverflow.com/questions/70224907/… Commented Feb 18, 2022 at 7:28
  • Sql Server is not used as the service is configured to mysql. I havent tried going from MySql 6 to 5 ill do that now Commented Feb 18, 2022 at 8:54
  • Try to use Pomelo.EntityFrameworkCore.MySql package - it is much widely used (19M downloads vs 737K of Oracle package) Commented Feb 20, 2022 at 6:08
  • I have found a repos with the use of pomelo so I will try that soon. cheers for the help lads Commented Feb 21, 2022 at 23:59

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I have added this class to data layer csproj which includes dbcontext and then Add-Migration worked successfully and added the migration folder and classes. Thx. (I use the latest EFCore version)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.