This is a simple CRUD operation for an Employe using ASP.NET Core 3.1 and EntityFrameworkCore
Prerequisites:
- Visual Studio 2019 IDE for Mac
- Make sure you are running the latest .Net core 3.1
- Install the latest DOTNET and EF CLI using this command: dotnet tool install --global dotnet ef
- MySQL database should be installed on your Mac
Getting Started:
Open the Visual Studio 2019 for Mac and Create a New Project as below
Please select the .Net Core 3.1 as below
I created a new project with the Name, "DotNetCore" and below is the Solution structure which
is created by VS2019.
1. Microsoft.EntityFrameworkCore
2. Microsoft.EntityFrameworkCore.Design
3. Microsoft.EntityFrameworkCore.Tools
4. MySql.Data
5. MySql.Data.EntityFrameworkCore
Now let's add a New Class name "DotNetCoreDbContext" under the Models folder by right-clicking
and select Add New Class.
Now let's write the code for this class.
We have to inherit our class from the DbContext class by using Microsoft.EntityFrameworkCore
and modify the Startup.cs file as below
Next step to add the Migration to create the Database in MySql
We have written the code to create and edit an employee, added the view page also.
Below are the screenshots for Index, Details, and Delete action methods
Run below command w.r.t your project folder path on Terminal
To Add-Migration:
dotnet ef migrations add InitialCreate
To Update-Database
dotnet ef database update
To Remove-Migration:
dotnet ef migrations remove
After adding the Migration, we can see our database has been created into the MySql database
We can see our created database dotnetcore including the Employees table.
Let's write the code for AddOrEdit action into EmployeeController
Now let's add the view page for AddOrEdit action under the Views/Employee folder Now let's build our solution and run the application.
Let's create 3-4 employees by entering the required fields as below
Let's write some action methods to Index, Details, and Delete an employee as well
https://localhost:5001/Employee/Index
Please comment if you face any issue regarding the same...
You can download the source code from GitHub by click on the below link
Below is the video of my running application: