Ricardo Sanchez Santos
Ricardo Sanchez Santos

Reputation: 544

Can't Connect with MySql, .Net Core Api

I'm new to this and I can't connect my API with my data base, i'm using .Net Core 3, MySql Server 8.0.18, also I downloaded the Connector/Net 8.0.18 and visual studio 2019. This is my string connection:

"Server=localhost,3306;Database=portaldb;user=****;password=****"

My Startup.cs :

public void ConfigureServices(IServiceCollection services)
    {
        var connection = Configuration.GetConnectionString(Defaultconnection);
        var mappingConfig = new MapperConfiguration(mc => { 
            mc.AddProfile(new MappingProfile());
        });
        var mapper = mappingConfig.CreateMapper();
        services.AddControllers();
        services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
                builder =>
                {
                    builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod();
                });
        });
        services.AddSingleton(mapper);
        services.AddTransient<IDbContext, PortalContext>();
        services.AddTransient<ISqlRepository, SqlRepository>();
        services.AddDbContext<PortalContext>(options => options.UseMySQL(connection));
    }

My DbContext:

public class PortalContext : DbContext, IDbContext
{
    public PortalContext(DbContextOptions options) : base(options)
    {

    }
    private DbSet<UsersModel> Users { get; set; }

    private DbSet<CandidatesModel> Candidates { get; set; }

    private DbSet<UserTypesModel> UserTypes { get; set; }

    private DbSet<CompetenciesModel> Competencies { get; set; }

    private DbSet<JobsModel> Jobs { get; set; }

    private DbSet<SkillsAssessmentsModel> SkillsAssessments { get; set; }

    private DbSet<SkillSetModel> Skills { get; set; }

    ///some methods
}

Here are some screenshots of the usings of my DbContext and the packages that I installed, i'm not sure if those are the right ones or if i'm not using them in the correct way.

Usings of my DbContext

Packages

MySql installations

Right now i'm getting this error

"Internal connection fatal error."

I tried with different connection string getting differents errors and I tried to search for an answer but there's almost no information with MySql, a lot with Sql server but no with MySql.

Edit I change the string connection and the startup but i'm still getting an error

: 'Method 'get_Info' in type 'MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.18.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.'

Edit 2 Ok i read that Microsoft.EntityFrameworkCore 3 it's not compatible with MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18so i did a rollback to the Microsoft.EntityFrameworkCore 2.1 and now i'm getting Unable to connect to any of the specified MySQL hosts.

Upvotes: 1

Views: 3512

Answers (2)

MOHIT AGARWAL
MOHIT AGARWAL

Reputation: 114

MySql.Data.EntityFrameworkCore supports and is only compatible with EF Core 2.1. For EF Core 3.0 or the latest version use Pomelo.EntityFrameworkCore.MySql

Upvotes: 0

Ricardo Sanchez Santos
Ricardo Sanchez Santos

Reputation: 544

OK, as Panagiotis Kanavos mentioned i was using a wrong connection string so i change it to:

Server=localhost;Database=portaldb;user=***;password=***

also as Dongdong said i was using options.UseSqlServer(connection) instead of services.AddDbContext<PortalContext>(options => options.UseMySQL(connection));

And with that i just have to change some versions, Microsoft.EntityFrameworkCore 3 it's not compatible with MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18, i did a rollback to Microsoft.EntityFrameworkCore 2.1 and now it works.

Upvotes: 1

Related Questions