Peter
Peter

Reputation: 11890

UseMySQL extension method not recognized in ASP.NET core

Environment: Ubuntu 16.04, .NET Core 1.10 (1.0.0-preview2-1-003177), Visual Studio Code 1.8.1

I have created an ASP.NET MVC Core application by running the following command:

$ dotnet new -t web

I am able to load the folder in VSC and debug it.

By default, the engine generates code for Sqlite. I am changing it to use MySQL. My changes are based on the information from the following two articles:

http://insidemysql.com/howto-starting-with-mysql-ef-core-provider-and-connectornet-7-0-4/ https://damienbod.com/2016/08/26/asp-net-core-1-0-with-mysql-and-entity-framework-core/

First, I added the following lines into dependencies section of project.json.

"MySql.Data.Core" :"7.0.4-ir-191",
"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"

After running dotnet restore, the required DLLs were downloaded.

The next step was to modify Startup.cs and modify

 services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

to

 services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

Essentially, I am replacing UseSqlite by UseMySQL.

However, extension method UseMySQL or UseMySQL do not seem to be available on DbContextOptionsBuilder.

Wondering if I missed some step somewhere. Regards.

Upvotes: 10

Views: 19275

Answers (3)

Shaun Luttin
Shaun Luttin

Reputation: 141462

Add the using MySql.Data.EntityFrameworkCore.Extensions; statement.

Upvotes: 15

Farid Huseynov
Farid Huseynov

Reputation: 159

I also had the same issue even though was installing the Pomelo.EntityFrameworkCore.MySql, just installed the package through the terminal:

dotnet add package Pomelo.EntityFrameworkCore.MySql

it worked!

Upvotes: 1

Ernest
Ernest

Reputation: 2209

I've successfully used this in my Mac with the latest version of VS Community in to an ASP CORE MVC project and I just needed to add the following NuGet-Packages:

Microsoft.EntityFrameworkCore  
MySql.Data.EntityFrameworkCore

Then using them on your class like:

using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;

Notice the MySQL on the USING statement is CAPITAL letter, and it is MySql on the NuGet Package name. Hope this hel

Upvotes: 4

Related Questions