What Is EF Core Database First Approach in Asp.Net Core MVC

What is entity framework database first approach in asp.net core mvc. How to connect and update the table object by Scaffold in asp.net core mvc, c#.

What Is EF Core Database First Approach in Asp.Net Core MVC
In today's article I will show you a simple tutorial with example what is Entity farmwork core data base approach in asp.net core mvc and how you can connect to a MS SQL database to your .NET core mvc application and get record from table using c#.net.

What is DB first In Entity Framework .NET Core?

DB first we use where we already have the database and with the help of mS SQL Database we create the model class which hold the detail of the tables in the database. This approach is helpful where the DB change is minimal.

Connect to MS SQL Database with EF Core in Asp.Net Core MVC

To connect to MS SQL server database using database first approach we ned to install the below mention three NuGet packages. 
Package to install 1. Microsoft.EntityFrameworkCore.Tools
2. Microsoft.EntityFrameworkCore.Design
3. Microsoft.EntityFrameworkCore.SqlServer
After installing the NuGet Packages your packages section will looks as shown below.

NuGet Package for EF Core

After this we will create a new database and add some tables in it.

SQL Database table

After installing all the 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
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.
So before running the command we need to create a folder which we have defined in the Scaffold. 
Folder for EF core


Now lets run the Scaffold command in Package Manager. 

Scaffold Command in Package Manager

After successful execution. We will get out table object as shown below.

EF Core Table Object
Now we have done. Before closing it, we will check how we can update the table object if there is any change. To update the table object for Entity Framework core in asp.net core mvc application we need to make a small change in the Scaffold command. We just need to add the -force at the end of the Scaffold command. Please refer the below command.
Scaffold command to update Table Object Scaffold-DbContext "Server=<MS SQL Server Name>;Database=<Database name>;Trusted_Connection=True;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data -force

Post a Comment