'How to connect to MySQL server using Entity Framework Core

I am using Visual Studio 2019 community edition. I want to connect to MySQL using Entity Framework core. I do not want to use Entity Framework 6.

I am running into following issues:

  1. I created a new project using "ASP .NET CORE App and ASP .NET CORE Web App" template and it does not show option to add Entity Framework.

enter image description here

  1. If I use tools > Connect to Database option from menu, I do not see option to connect to MySQL. How can I enable this option.

enter image description here



Solution 1:[1]

Install "Pomelo.EntityFrameworkCore.MySql" using the NuGet Package Manager in Visual Studio 2019.

Then follow the instructions to configure it in your project: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/#getting-started

Disclaimer: I have made a small number of contributions to the Pomelo package.

Solution 2:[2]

Step by Step - Database First

First install these packages

  1. Microsoft.EntityFrameworkCore.Design

So on Powershell go to project folder [right click on project and select open in terminal (visual studio)]

enter image description here

Now, you can run this command

enter image description here

dotnet ef dbcontext scaffold "Servel=localhost;Database=tempSQLonNetCore;user=root;password=;" "Pomelo.EntityFrameworkCore.MySql"

If your connection is true, your DbContext Generated and entities adding to your project.

Now you must inject DbContext, Described in the Codefirst section


Step by Step - Code First

First install these packages

  1. Microsoft.EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore.Tools
  3. Pomelo.EntityFrameworkCore.MySql

Add connection string look like in appsetting.json

"ConnectionStrings" : {
    "DefaultConnection" : "Servel=localhost;Database=tempSQLonNetCore;user=root;password=;"
}

**Now, Create your DB context **

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }

    // Your Entities
}

finally configure the app for connecting

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options => {
     options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});

Now you can use Migration if you need to create or update your database Migration

Solution 3:[3]

EFCore is a package, not a file you would add to your project. Go to Project>NuGet Package Manager and install Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Tools. Then you can scaffold the connection to your database by running the scaffold-dbcontext command in the PM console.

Solution 4:[4]

did you try installing through the console?

Install-Package Microsoft.EntityFrameworkCore -Version 5.0.11

make sure that this source is added to your nuget sources in VS https://api.nuget.org/v3/index.json

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 thisisnabi
Solution 3 Garrett
Solution 4 Hilal