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.
.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
- 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.
I’m using the environment below for this post:
- MacOS High Sierra
- PhpMyAdmin for MySQL
- Visual Studio Code
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
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:
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
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
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 ….!!
HI, WHEN I DID THAT , nothing happened and it show build failed
It means there are some runtime errors in your solution.
hello, How can I use Ado.net with this project and EF is faster or Ado.Net is faster? Which approach do you prefer?
If you’re working on an MVC Project, you should use EF.
When I want to update models I got “Build failed”.
It means you have a compile-time error in your Code. Please first simply try to run -> dotnet build
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.
Comments are closed.