Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.ServerVersion

I'm using C# and have an error: 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; }
            }
        }
    }
like image 868
TheRain Avatar asked Mar 20 '21 10:03

TheRain


2 Answers

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

This should help: https://metanit.com/sharp/entityframeworkcore/7.2.php

like image 134
TheRain Avatar answered Nov 01 '22 08:11

TheRain


Apparently, new version of Pomelo needs more arguments. A simple solution is to add an empty Version class like this in startup.cs:

services.AddDbContext<ApplicationDB>(options =>
                   options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion(new Version())));
like image 2
ehsan_kabiri_33 Avatar answered Nov 01 '22 07:11

ehsan_kabiri_33