Home > Blockchain >  I have problem with GetConnectionString in ASP.NET MVC
I have problem with GetConnectionString in ASP.NET MVC

Time:03-10

I have a problem connecting a database using ASP.NET MVC and the identification system. I want to connect to mySQL.

First, I launched xampp, next definition connection string. And then in the Configure Services method, I used UseMySQL instead of the default connection.

If I enter the default connection in various forms, I have throw error that the string cannot be converted to Microsoft.EntityFrameworkCore.Serverversion.

I found a similar problem and wrote MySqlServerVersion, but it doesn't work. GetConnectionString doesn't take 2 arguments.

https://i.stack.imgur.com/gIH2s.png

https://i.stack.imgur.com/zFlPI.png

Could someone please tell me what I am doing wrong? And sorry for my English.

CodePudding user response:

UseMySql takes 2 arguments, the ConnectionString and the Version (optional). You're trying to set the version when you get the connection string, simply move your parentheses up a level:

options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion(new Version(8, 0, 11))

CodePudding user response:

First install:

Pomelo.EntityFrameworkCore.MySql

And You can register your service as shown below. Note that there is no need to configure the server version Manualy

 services.AddDbContext<ApplicationDbContext>(options =>
    {
        var connetionString = Configuration.GetConnectionString("DefaultConnection");
        options.UseMySql(connetionString, ServerVersion.AutoDetect(connetionString));
    });
  • Related