creating models from database
| | | |

Entity Framework Core Database First – Asp.Net Core Scaffold MySql DB

This post will guide you on how to generate Models using Database/Model First Approach and Scaffold MySql Database in Asp.Net Core using Mac.

History

.Net Core is a lightweight and cross-platform version of the DotNet framework and the awesome thing is Developers required the same expertise to work with Asp.Net Core as .Net Framework.

After .Net Core Launch, Mac, and Linux users can also develop using Microsoft Technologies.

Most of the users use SQL Server as a Database of their Application when working with .Net Technologies. But difficulties occur while developing with macOS or Linux, Although SQL Server can be used using Docker which is very popular these days, Docker required some expertise that’s why most of the developers love to use other Databases.

When .Net Core was released the framework supported only the following databases:

  • SQL Server
  • Postgres
  • SQLite
  • SQL Server Compact

But with a redesigned completely, EntityFramework Core became lighter than the old version. Now in its 2nd version, EntityFramework Core supports more databases and in this article, I will show you how to use it with a MySQL database.

Environment

I’m using the environment below for this post:

  • MacOS High Sierra
  • PhpMyAdmin for MySQL
  • Visual Studio Code

Solution

I’m using Visual Studio Code

First, create a DotNet Core Console Application using DotNet CLI Command

dotnet new console -o DotNetCoreMySQL

My default .Net Core version is 2.2. So, my project will be created in 2.2.

Now Add Pomelo Nuget Package, your project compatible version.
dotnet add package Pomelo.EntityFrameworkCore.MySql

For EntityFramework Core Commands, add the line below to your project configuration file DotNetCoreMySQL.csproj

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
</ItemGroup>

run this command

dotnet restore

Now run the scaffold command to create Models from Database

dotnet ef dbcontext scaffold "Server=localhost;User Id=root;Password=1234;Database=MyTestDB" "Pomelo.EntityFrameworkCore.MySql" -c MyDbContext

Your Models will be created. I recommend you first to create Models Folder and scaffold your Database there.

use these commands to that:

mkdir Models

Now run this scaffold command to scaffold inside Models folder.

dotnet ef dbcontext scaffold "Server=localhost;User Id=root;Password=1234;Database=MyTestDB" "Pomelo.EntityFrameworkCore.MySql" -c MyDbContext -o Models

Update Models

You can use Scaffold-DbContext command with -f flag. This way you can force scaffolding to overwrite existing model files.

dotnet ef dbcontext scaffold "Server=localhost;User Id=root;Password=1234;Database=MyTestDB" "Pomelo.EntityFrameworkCore.MySql" -c MyDbContext -f

Most of the developers face problem in scaffolding due to versions conflict. Keep in mind, if you’re using .Net 2 then use the Pomelo of the same version or if you are working with .Net Core 2.1 then use the Latest version of Pomelo.

If you still face any problem please comment below.

Here are more Articles you might be Interested

– Creating Admin Panel in Asp.net Core MVC – Step by Step Tutorial

– Top 10 .Net Core Features You need to know

– Dynamic Role-Based Authorization Asp.net Core

– Upload File to SFTP Server using C#

Similar Posts

9 Comments

  1. Amazing example. I didn’t know that it can be simple like this. Earlier we were working on SqlServer but we need to shift to MySQL. I tried so many things to generate, But your blog only solved my problem.
    Thank you ….!!

  2. hello, How can I use Ado.net with this project and EF is faster or Ado.Net is faster? Which approach do you prefer?

  3. hi, very good example. how we can generate navigation properties or relationship between tables using scaffold ? i’ve tried scaffold, it only generate models.

Comments are closed.