This post will guide you 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

Now Add this library
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 2.0.1

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" />
</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#

Author

I'm passionate about learning new technologies as well as mentoring and helping others get started with their programming career. This blog is my way of giving back to the Community.

8 Comments

  1. oussama chaabouni Reply

    HI, WHEN I DID THAT , nothing happened and it show build failed

  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. SYAHMAN MOHAMAD Reply

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

    • As we know that this is Database First Approach. Generate the relations in you Database then scaffold. It will also be generated in your Models.

Write A Comment