Asp.Net Core MVC: Entity Framework

How to user Entity Framework in asp.net core mvc using c#.net from MS SQL server database. Select data from database using LINQ in c#.net.
In today's article I will show you a simple tutorial with example. How you can use an entity framework and make connection with ms sql server database with your asp.net core mvc application using entity framework using c#.net. Now for this first we will create a new ms sql server database and add some table.

Ms sql server database

Now let's check the data of table Employee. After making connection we will user this table to get the data.

employee table

Here we will use the above table in our example and here we will connect to database and show to get data demo database. Now we will add below mention NuGet package in our application. To add a NuGet package we need to expand Solution Explorer, in this Right Click on Dependencies -> Manager NuGet Packages. 

Manage Nuget Package

On click of Manage NuGet Package, below screen will open in this we need to search for below mention package.

  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.SqlServer

After installing the given three packages we it will reflect in Dependency folder. As shown below.

NuGet Packages

After installing all three packages we will be needed to run the Scaffold DBContext command. So what is Scaffold command, it is the command which we use for DB first approach to make connection with the DB and generate the model class for. Please refer the below screen for the syntax of the Scaffold. This command we run through Project Manager Console.
Scaffold command syntax Scaffold-DbContext "Server=<MS SQL Server Name>;Database=<Database name>;Trusted_Connection=True;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data -force

Above syntax describe the complete command detail which we cna execute. Here we are having following items.

Server: <MS SQL Server Name>;
Database: <Database name>;
Trusted_Connection: True;
TrustServerCertificate:True; 
To avoid the below mention error Trusted_Connection=True have been used.
Error! A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)
Microsoft.EntityFrameworkCore.SqlServer: Represent the provider for the MS SQL server
-OutputDir data: This shown the folder in root. So this folder will have all the table model class files and the DBContext class file after execution of the Scaffold command.
Now we will create a new folder in names as "data" you can create as per your naming convention. After creating folder, we will run Scaffold command. 

Scaffold-DbContext

Now we will check the solution explorer for data folder. Here we are able to see the table object class files. One file having name EmployeeContext. We will be able to access all the table object with object of EmployeeContext. 

Table Object

Now we will create access value from employee talbe. Pleasse refer the below code. 
 public IActionResult Index()
 {
     EmployeeContext employeeContext = new EmployeeContext();
     var data = employeeContext.Employees.ToList();
     return View();
 } 
Now run the code to check the output. Here we can see we are able to get the record from employee table.
Data from database

Post a Comment