Create Asp.Net Core MVC Project

In Visual Studio, Goto File > New > Project ( Ctrl + Shift + N). Then select Asp.Net Core Web Application.

In template wizard, Select Web Application( MVC ) template. Make sure to select latest Asp.Net Core Version from top dropdown. We don’t need to configure this application with HTTPS (Unchecked corresponding option from bottom).

Setup Database for EF Core

For this application development, we will use EF Core – Code First Approach. First of all, we have to install NuGet Package for EFCore. for that you can right click on project in solution explorer, click on Manage NuGet Packages. In Browse tab, search for Microsoft.EntityFrameworkCore. Install the package with same version as that of Asp.Net Core.

We will create the DBContext class inside Models folder. To demonstrate Asp.Net Core CRUD Operation, we will deal with employee details like Full Name, Employee Code,Position and Office Location. So I have named the DbContext as EmployeeContext.

Copy to Clipboard

public class Employee
{
    [Key]
    public int EmployeeId { get; set; }

    [Column(TypeName ="nvarchar(250)")]
    [Required(ErrorMessage ="This field is required.")]
    [DisplayName("Full Name")]
    public string FullName { get; set; }

    [Column(TypeName = "varchar(10)")]
    [DisplayName("Emp. Code")]
    public string EmpCode { get; set; }

    [Column(TypeName = "varchar(100)")]
    public string Position { get; set; }

    [Column(TypeName = "varchar(100)")]
    [DisplayName("Office Location")]
    public string OfficeLocation { get; set; }
}

Inside the class, we have a DbSet property for Employees Collection of the type Employee class. So we have to define the class with required properties as follows.

#asp.net core #asp.net core article #asp.net core 2.2 #asp.net core crud

Asp.Net Core MVC CRUD with EF Core
5.50 GEEK