How to insert console application with Entity Framework

Introduction

 
In this tutorial, we will develop a simple C# console application that we can insert using Entity Framework Core.
 

Getting Started

 
In ASP.NET 5 we can create console applications. To create a new console application, first, open Visual Studio 2015. Create the application using File-> New-> Project.
 
visual-studio-creat-project
 
Now we will select the ASP.NET 5 Console Application and click on the OK button.
 
visual-studio-console-application
 
We’re going to use the Entity Framework Designer included as a part of Visual Studio.
 
Step 1: Click on Project -> Manage NuGet Package.
 
create-project-visual-studio
 
Use the below table query to executes any database in the SQL Server.
  1. CREATE TABLE [dbo].[Departments](   [DeptId] [int] NOT NULL,    
  2.     [DeptName] [varchar](50) NULL,    
  3.  CONSTRAINT [PK_Departments] PRIMARY KEY CLUSTERED     
  4. (    
  5.     [DeptId] ASC    
  6. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]    
  7. ) ON [PRIMARY]    
  8.     
  9. GO    
  10.     
  11. CREATE TABLE [dbo].[Employees](    
  12.     [EmpId] [int] NOT NULL,    
  13.     [EmpName] [varchar](50) NULL,    
  14.     [DeptId] [int] NULL,    
  15.  CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED     
  16. (    
  17.     [EmpId] ASC    
  18. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]    
  19. ) ON [PRIMARY]    
  20. GO   

We need to include the following references in our application:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.EntityFrameworkCore;  
  4. using System.ComponentModel.DataAnnotations; 
In the main method, I have created 3 classes and 1 method.
  1. Department – To define class members.  
  2. Employee – To define class members.  
  3. EagerLoadingDbContext= Initialize dB context in the class.  
  4. SavedDatabaseOperations– Saved Operation in this method. 
Please refer the code snippet below.
  1. public static void Main(string[] args)  
  2. {  
  3. try  
  4. {  
  5.         SavedDatabaseOperations();  
  6.         Console.Write("Data is saved successfully !");  
  7.     }  
  8. catch (Exception ex)  
  9.     {  
  10.         Console.WriteLine("Error:" + ex.Message);  
  11.     }  
  12. Console.ReadKey();  
  13. }  

Initialize the class

 
The first-class definition for the Department is given below.
  1. public class Department  
  2.     {  
  3.         [Key]  
  4.         public int DeptId { getset; }  
  5.   
  6.         public string DeptName { getset; }  
  7.     } 
The second-class definition for the Employee is given below.
  1. public class Employee  
  2.     {  
  3.         [Key]  
  4.         public int EmpId { getset; }  
  5.   
  6.         public string EmpName { getset; }  
  7.   
  8.         public int DeptId { getset; }  
  9.     } 
The Third-class definition for the EagerLoadingDbContext is given below.
  1. class EagerLoadingDbContext : DbContext  
  2.     {  
  3.   
  4.         private const string connectionString = "Your own Sql-Server connection string";  
  5.   
  6.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
  7.         {  
  8.             optionsBuilder.UseSqlServer(connectionString);  
  9.         }  
  10.   
  11.         public DbSet<Department> Departments { getset; }  
  12.   
  13.         public DbSet<Employee> Employees { getset; }  
  14.     } 

Initialize the Method

 
The method definition for the SavedDatabaseOperations() is given below.
 
  1. public static void SavedDatabaseOperations()  
  2.         {  
  3.             using (var db = new EagerLoadingDbContext())  
  4.             {  
  5.                 var department = new List<Department>  
  6.                 {  
  7.                     new Department{ DeptId = 1,DeptName = "IT"},new Department{DeptId = 2,DeptName = "HR" },new Department{DeptId = 3,DeptName = "Marketing" }  
  8.                 };  
  9.   
  10.                 db.Departments.AddRange(department);  
  11.   
  12.                 var employee = new Employee  
  13.                 {  
  14.                     EmpId = 1,  
  15.                     EmpName = "Nirmal",  
  16.                     DeptId = 1,  
  17.   
  18.                 };  
  19.   
  20.                 db.Employees.Add(employee);  
  21.   
  22.                 db.SaveChanges();  
  23.             }  
  24.         } 
Click on F5 and execute the project and follow the console window. The output will be:
 
output-image1
 
output-image2
   

Summary

 
I hope this article helps while trying to perform an insert using Entity Framework Core Data Model. Thanks for reading this article!