Create an Entity Framework Core Model

Employee Manager performs database CRUD operations using Entity Framework Core. To work with EF Core, you need to build an EF Core model

Step 1 : Let’s create the model required for the Employee Manager application. Open the Manage NuGet Packages using the Tools > NuGet Package Manager > Manage NuGet Packages for Solution… and add these packages to your project:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Relational

Step 2 : Now add a folder named Models in the project. Then using the Add New Item dialog, add three classes to the Models folder, namely, Employee, Country, and AppDbContext.

Step 3 : Update the Models/Employee.cs file


...
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace EmployeeManager.Models
{
    [Table("Employees")]
    public class Employee
    {
        [Column("EmployeeID")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Required(ErrorMessage = "Employee ID is required")]
        [Display(Name = "Employee ID")]
        public int EmployeeID { get; set; }


        [Column("FirstName")]
        [Display(Name = "First Name")]
        [Required(ErrorMessage = "First Name is required")]
        [StringLength(10, ErrorMessage = "First Name must be less than 10 characters")]
        public string FirstName { get; set; }


        [Column("LastName")]
        [Display(Name = "Last Name")]
        [Required(ErrorMessage = "Last Name is required")]
        [StringLength(20, ErrorMessage = "Last Name must be less than 20 characters")]
        public string LastName { get; set; }


        [Column("Title")]
        [Display(Name = "Title")]
        [Required(ErrorMessage = "Title is required")]
        [StringLength(30, ErrorMessage = "Title must be less than 30 characters")]
        public string Title { get; set; }


        [Column("BirthDate")]
        [Display(Name = "Birth Date")]
        [Required(ErrorMessage = "Birth Date is required")]
        public DateTime BirthDate { get; set; }


        [Column("HireDate")]
        [Display(Name = "Hire Date")]
        [Required(ErrorMessage = "Hire Date is required")]
        public DateTime HireDate { get; set; }


        [Column("Country")]
        [Display(Name = "Country")]
        [Required(ErrorMessage = "Country is required")]
        [StringLength(15, ErrorMessage = "Country must be less than 15 characters")]
        public string Country { get; set; }


        [Column("Notes")]
        [Display(Name = "Notes")]
        [StringLength(500, ErrorMessage = "Notes must be less than 500 characters")]
        public string Notes { get; set; }
    }
}    

Step 4 : Update the Models/Country.cs file


    public class Country
    {
        public int CountryID { get; set; }
        public string Name { get; set; }
    }

Step 4 : Update the Models/AppDbContext.cs file


using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;

namespace EmployeeManager.Models
{
    public class AppDbContext : DbContext
    {

        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }


        public DbSet<Employee> Employees { get; set; }

        public DbSet<Country> Countries { get; set; }

    }
}        

Step 5 : Register AppDbContext in the Startup class. Then, pass the connection string and database provider as parameters within the constructor


...
using Microsoft.Extensions.Configuration;
using EmployeeManager.Models;
using Microsoft.EntityFrameworkCore;
...
private IConfiguration config = null;

public Startup(IConfiguration config)
{
    this.config = config;
}

public void ConfigureServices(IServiceCollection services)
{

    var connectionString = this.config.GetConnectionString("AppDb");
    services.AddEntityFrameworkSqlServer()
            .AddDbContext<AppDbContext>((serviceProvider, options) => options.UseSqlServer(connectionString)
            .UseInternalServiceProvider(serviceProvider)
    );
}
...

Step 6 : Store the Database Connection String in appsettings.json. Add the following lines inside the appsettings.json file.

"ConnectionStrings": {
    "AppDb": "Data Source=192.168.1.95;Initial Catalog=Northwind;Persist Security Info=False;User ID=sa;Password=123"
}

The data source specifies the name or IP address of the SQL Server installation. Here, 192.168.1.95. The initial catalog specifies the name of the database you want to connect with, Northwind in this case.

Make sure to change this connection string as per your setup. Storing the connection string inside a configuration file makes it possible to change it at a later stage without needing any change to the source code.

Add Comment