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
Recommended Training – Treehouse
From beginner to advanced, our recommended coding training is Treehouse.
Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.
Treehouse's mission is to bring technology education to those who can't get it, and is committed to helping its students find jobs. If you're looking to turn coding into your career, you should consider Treehouse.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, we will receive an affiliate commission. Regardless, we only recommend products or services we use personally and believe will add value to our readers.