【问题标题】:asp.net 6 EF using MySql Database not working使用 MySql 数据库的 asp.net 6 EF 不起作用
【发布时间】:2022-04-27 10:08:02
【问题描述】:

我正在尝试将我在 asp.net 6 中构建的 Web API 与 MySql 连接,我遇到了很多麻烦

我的服务看起来像这样

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

我的数据库上下文是这样的。

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; }
        

    }
}

当我运行 add-migration initial 时出现错误

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

任何帮助将不胜感激。

请帮助我完全迷路了

大家干杯

【问题讨论】:

  • 是否也安装了所有依赖项?您不需要 SQLServer 包,除非您同时针对两个数据库。您是否尝试过使用 v5 而不是 v6?例如stackoverflow.com/questions/70224907/… 之类的问题
  • 没有使用Sql Server,因为服务配置为mysql。我还没有尝试过从 MySql 6 到 5 我现在这样做了
  • 尝试使用Pomelo.EntityFrameworkCore.MySql 包 - 它被广泛使用(19M 下载 vs 737K 的 Oracle 包)
  • 我找到了一个使用 pomelo 的存储库,所以我会尽快尝试。为帮助的小伙子欢呼

标签: c# mysql asp.net webapi .net-6.0


【解决方案1】:

所以在调整之后我有了解决方案。 使用 pomelo 包和这条线。

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

希望这对开发者有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2013-01-29
    • 1970-01-01
    • 2016-01-31
    • 2017-09-26
    相关资源
    最近更新 更多