4

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

1
  • perhaps you need to install mysql for visual studio for it to appear there. Commented May 6, 2022 at 23:30

4 Answers 4

10

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

I will not propose official package also. It do not supports async operations and just emulates them.
0

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.

1 Comment

Or, as a less headache way of scaffolding, install the EF Core Power Tools VS extension marketplace.visualstudio.com/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.