Update Table Object Changes for SQL Serve in Asp.Net Core MVC, C#.Net

How to update your db context table object in your asp.net core mvc application using DB first approach, Entity framework from SQL server database.
In today's article I will show simple tutorial with an example how you can update already available entity framework DB context or table objects changes made in SQL server database. To make the connection with SQL database in Asp.Net Core MVC and get database object we will use Scaffold. So, for this article first we will SQL server database and by using DB first approach we will make our DB context at our application end. 

MS SQL Table
Here in above image, we are having two tables first one is Employee and second one is Projects. Now let consider we have already included the table in our asp.net core mvc application.

DB Entity context

In above we can see the tables in the folder. Now let's add some more table and views in the  SQL database. 

New Table and View in SQL

We have added two new item one is table and second one is SQL View. Now let's check the Scaffold command.

Scaffold command syntax for first execution 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; 

The above command is for running the command first for the application, now to update on second time when we are running, we need to add -force at the end of the Scaffold command. Please check the modified command.
Scaffold command syntax for update Scaffold-DbContext "Server=<MS SQL Server Name>;Database=<Database name>;Trusted_Connection=True;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data -force
Now to update the changes in DB we need to run the above (by making change as per your server derail) command from Package manager console.

Scaffold Command

Now we have done by running the command. Now let's check the solution explorer for changes. 

DB context Solution Explorer

In above we can see that we have got the newly added table and view detail.  So, this way you can update your DB contact in your development environment. Remember this is not a migration. 

Post a Comment