Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0

Today I will explain how to install entity framework core in your ASP.NET MVC core 3.0 application with an empty template. Entity Framework Core, also called EF Core, is a complete rewrite from the ground up. If you have any experience with previous versions of Entity Framework, you will find lot of familiar features.

What is EF core?

 
EF Core is an ORM (Object-Relational Mapper). EF core is lightweight, extensible, and open source software. Like .NET Core, EF Core is also cross platform. It works on windows, Mac OS, and Linux. EF core is Microsoft’s official data access platform.
 

What is ORM?


ORM stands for Object-Relational Mapper and it enables developers to work with a database using business objects. As a developer we work with the application business objects and the ORM generates the SQL that the underlying database understands. In-short, an ORM eliminates the need for most of the data-access code that developers usually need to write
 

EF Core Code First Approach


EF Core supports both Code First approach and Database First approach. However, with the Database First approach there is very limited support in EF core at the moment.
 
Step 1
 
Startup Visual Studio 2019. Choose ASP.NET Core Web Application and click on “Next”
 
 Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0
 
After clicking next, another wizard will open. Under the project name, give a meaningful name to your project and click on create.
 
Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0 
 
That will open up another new wizard select ASP.Net Core 3.0 from the dropdown. If not, select default. Choose an empty template and click on create which will create your first ASP.Net Core Application.
 
Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0 
Step 2
 
Click on tools select nuget Package manager than click on manage nuget package for solution. Now click on browse tab than search following package and install them.
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Relational
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer
Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0 
 
Step 3
 
Now create a folder Models under your project and create Employee class and EmployeeDbContext class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace MvcCoreApplication.Models  
  8. {  
  9.     public class Employee  
  10.     {  
  11.         [Key]  
  12.         public int Id { getset; }  
  13.   
  14.         [Required(ErrorMessage ="Please enter first name")]  
  15.         [Display(Name ="First Name")]  
  16.         [StringLength(100)]  
  17.         public string FirstName { getset; }  
  18.   
  19.         [Required(ErrorMessage = "Please enter last name")]  
  20.         [Display(Name = "Last Name")]  
  21.         [StringLength(100)]  
  22.         public string LastName { getset; }  
  23.   
  24.         [Required(ErrorMessage = "Please choose gender")]  
  25.         [StringLength(10)]  
  26.         public string Gender { getset; }  
  27.   
  28.         [Required(ErrorMessage = "Please enter age")]  
  29.         public int Age { getset; }  
  30.   
  31.         [Required(ErrorMessage = "Please enter position")]  
  32.         [StringLength(100)]  
  33.         public string Position { getset; }  
  34.   
  35.         [Required(ErrorMessage = "Please enter office")]  
  36.         [StringLength(100)]  
  37.         public string Office { getset; }  
  38.   
  39.         [Required(ErrorMessage = "Please enter first name")]  
  40.         public int Salary { getset; }  
  41.     }  
  42. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.EntityFrameworkCore;  
  6.   
  7. namespace MvcCoreApplication.Models  
  8. {  
  9.     public class EmployeeDbContext:DbContext  
  10.     {  
  11.         public EmployeeDbContext(DbContextOptions options)  
  12.             :base(options)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         public DbSet<Employee> Employees { getset; }  
  18.     }  
  19. }  
Step 4
 
Now open appsettings.json file and add connect how we use set the connection in web config file. Similarly here you need to setup connect string.
  1. {  
  2.   "Logging": {  
  3.     "LogLevel": {  
  4.       "Default""Information",  
  5.       "Microsoft""Warning",  
  6.       "Microsoft.Hosting.Lifetime""Information"  
  7.     }  
  8.   },  
  9.   "AllowedHosts""*",  
  10.   "ConnectionStrings": {  
  11.     "DefaultConnection""server=(localdb)\\MSSQLLocalDB;database=DemoDB;Trusted_Connection=true"  
  12.   }  
  13. }  
Step 5
 
Visual Studio 2019 generates program and startup classes. Now open the startup file and configure ASP.Net MVC Core Application development. Under app.UseEndpoints method just map endpoints.MapControllerRoute. Add connection middleware in services.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Builder;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using Microsoft.AspNetCore.Http;  
  8. using Microsoft.EntityFrameworkCore;  
  9. using Microsoft.Extensions.Configuration;  
  10. using Microsoft.Extensions.DependencyInjection;  
  11. using Microsoft.Extensions.Hosting;  
  12. using MvcCoreApplication.Models;  
  13.   
  14. namespace MvcCoreApplication  
  15. {  
  16.     public class Startup  
  17.     {  
  18.         private readonly IConfiguration Configuration;  
  19.         public Startup(IConfiguration configuration)  
  20.         {  
  21.             Configuration = configuration;  
  22.         }  
  23.   
  24.           
  25.         public void ConfigureServices(IServiceCollection services)  
  26.         {  
  27.             //Add MVC Middleware  
  28.             services.AddControllersWithViews();  
  29.   
  30.             //Add Connection Middleware  
  31.             services.AddDbContext<EmployeeDbContext>(  
  32.                 options => options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection")));  
  33.         }  
  34.   
  35.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  36.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  37.         {  
  38.             if (env.IsDevelopment())  
  39.             {  
  40.                 app.UseDeveloperExceptionPage();  
  41.             }  
  42.   
  43.             app.UseRouting();  
  44.   
  45.             app.UseEndpoints(endpoints =>  
  46.             {  
  47.                 //Add MVC Default route  
  48.                 endpoints.MapControllerRoute(  
  49.                     name: "default",  
  50.                     pattern: "{contoller=Home}/{action=Index}/{id?}"  
  51.                     );  
  52.             });  
  53.         }  
  54.     }  
  55. }  
Step 6
 
Now go to tools select nuget package manage than click on package manager console. It will bring console window below in visual studio 2019. Now write the following commands to enable migration.
  • enable-migrations
  • add-migration InitialModel  (add migration can be anything you wish to)
  • update-database
 Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0
 
Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0 
  
Install Entity Framework Core With Empty Template In ASP.NET MVC Core 3.0 
 
Here is the complete code.