【问题标题】:Cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.ServerVersion无法从“字符串”转换为“Microsoft.EntityFrameworkCore.ServerVersion”
【发布时间】:2021-06-17 14:18:59
【问题描述】:

我正在使用 C# 并且有一个错误: Argument 2: cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.ServerVersion'

    using Microsoft.EntityFrameworkCore;
    using System;
    
    namespace Infrastructure
    {
        public class BotContext : DbContext 
        {
            public DbSet<Server> Servers { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder options)
                => options.UseMySql("server=localhost;user=root;port=3306;Connect Timeout=5;");
    
            public class Server
            {
                public ulong Id { get; set; }
                public string Prefix { get; set; }
            }
        }
    }

【问题讨论】:

  • 您使用的是什么 NuGet 包?我认为您使用的是Pomelo,但使用the documentation by MySQL 实例化它,所以选择oneother...
  • 是的,使用 Pomelo。我重写了 Pomelo 文档中的代码,但代码仍然不起作用(同样的错误)protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySql( "server=localhost;user=root;port=3306;Connect Timeout=5;" ); }
  • ???您提供的文档和链接清楚地显示了您需要传递的 second 参数......您似乎一直错过它。
  • 哈,真的很有帮助!非常感谢!

标签: c# mysql entity-framework-core


【解决方案1】:
public class BotContext : DbContext 
{
    public DbSet<Server> Servers { get; set; }
    public BotContext()
    {
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql(
            "server=localhost;user=root;port=3306;Connect Timeout=5;",
            new MySqlServerVersion(new Version(8, 0, 11))
        );
    }

    public class Server
    {
        public ulong Id { get; set; }
        public string Prefix { get; set; }
    }
}

这应该会有所帮助:https://metanit.com/sharp/entityframeworkcore/7.2.php

【讨论】:

【解决方案2】:

显然,Pomelo 的新版本需要更多参数。一个简单的解决方案是在 startup.cs 中像这样添加一个空的 Version 类:

services.AddDbContext<ApplicationDB>(options =>
                   options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion(new Version())));

【讨论】:

  • 你测试过这个吗?
猜你喜欢
  • 1970-01-01
  • 2022-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 2021-10-09
  • 2016-12-02
  • 1970-01-01
相关资源
最近更新 更多