Asp.Net Core MVC: Update Entity Framework for SQL Server

How to update entity framework objected and property of object in your asp.net core 8 mvc application using c#.Net, with Scaffold .
In today's article I will show a simple tutorial with and an example how you can update Entity Framework table object in an existing object. Here we will see if we are making any change in existing table object in SQL server and update ad a new table in existing SQL server database, on that case how we can update it.

So here is the article where I show how you can connect to SQL server in you new asp.net core mvc application:

So, for this article first we will create asp.net core mvc application. Here I am using asp.net core 8 mvc using c#.net. After creating the application, we will create a database with few tables. 

SQL Database

Now let's open one of the tables and check the fields. 

SQL Table Design

Here to connect to SQL server database from Asp.net core mvc application, we will use Scaffold command. Please check the detail of 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 time 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.

Table object in .net core

Now let's check detail of Project detail.

Table object property

Now at SQL server side we will create a new table and update project table.

New table object

Now let's run the scaffold command and check the output. 

Update table object for EF command

Now let's check solution explorer for new table object and table update

New table object

Post a Comment