无法连接 MySql、.Net Core Api

Can't Connect with MySql, .Net Core Api

我是新手,无法将我的 API 与我的数据库连接,我正在使用 .Net Core 3MySql Server 8.0.18,我还下载了 Connector/Net 8.0.18 和 visual studio 2019。这是我的字符串连接:

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

我的 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));
    }

我的 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
}

下面是一些使用我的 DbContext 和我安装的包的截图,我不确定这些是不是正确的或者我没有以正确的方式使用它们。

Usings of my DbContext

Packages

MySql installations

现在我收到这个错误

"Internal connection fatal error."

我尝试使用不同的连接字符串得到不同的错误,我试图搜索答案,但是 MySql 几乎没有任何信息,Sql server 很多,但 MySql 没有.

编辑 我更改了字符串连接和启动,但仍然出现错误

: '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.'

编辑 2 好的,我读到 Microsoft.EntityFrameworkCore 3 它与 MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18 不兼容所以我回滚到 Microsoft.EntityFrameworkCore 2.1 现在我得到 Unable to connect to any of the specified MySQL hosts.

好的,正如 Panagiotis Kanavos 提到的,我使用了错误的连接字符串,所以我将其更改为:

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

也正如东东所说,我用的是options.UseSqlServer(connection)而不是services.AddDbContext<PortalContext>(options => options.UseMySQL(connection));

然后我只需要更改一些版本,Microsoft.EntityFrameworkCore 3 它与 MySql.Data.EntityFrameworkCore for Entity Framework - 8.0.18 不兼容,我回滚到 Microsoft.EntityFrameworkCore 2.1 现在它可以工作了。

MySql.Data.EntityFrameworkCore 支持并仅兼容 EF Core 2.1。对于 EF Core 3.0 或最新版本,请使用 Pomelo.EntityFrameworkCore.MySql