How to update Entity Framework Core in ASP.Net Core application for Database First Approach

The database update is very common in any real-time application so sometimes we add a new table or update the existing table by adding a new column, therefore, applications using Entity Framework as an ORM have to update their Entity Data model as well.

Earlier, Entity Framework provided a nice GUI to update the Entity data model for Database First Approach but Entity Framework Core doesn't have any GUI to do the same and can be updated by running the command.

Following is the command to update Entity Data Model in Entity Framework Core by force as we don't have the option to update rather create again by force.

dotnet ef dbcontext scaffold "Data Source=DESKTOP-TJHLDSD\SQLEXPRESS;Initial Catalog=AdventureWorks2014;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer  --context-dir EFDbContext --output-dir EFDbContext --context DbCoreContext --force --table [HumanResources].[Employee]


If there is any schema change in the table then we can mention the table name with --table in the above command.

If a new table is added to the database and we need to update the entity data model then we can mention the table with a new --table option like below

dotnet ef dbcontext scaffold "Data Source=DESKTOP-TJHLDSD\SQLEXPRESS;Initial Catalog=AdventureWorks2014;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer  --context-dir EFDbContext --output-dir EFDbContext --context DbCoreContext --force --table [HumanResources].[Employee] --table [HumanResources].[Department]


For our convenience, we can create a batch file in the project folder so that we just need to double click and it will update the Entity data model and we don't need to remember such a long command.

scaffold.bat

dotnet ef dbcontext scaffold "Data Source=DESKTOP-TJHLDSD\SQLEXPRESS;Initial Catalog=AdventureWorks2014;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer ^
--context-dir EFDbContext --output-dir EFDbContext --context DbCoreContext --force ^
--table [HumanResources].[Employee] ^
--table [HumanResources].[Department] ^
--table [HumanResources].[EmployeeDepartmentHistory] ^
--table [HumanResources].[EmployeePayHistory] ^
--table [HumanResources].[JobCandidate] ^
--table [HumanResources].[Shift] ^
--table [Person].[Address]
cmd /k

scaffold_bat_file_ef_core.jpg



Hope this article will help you to update the Entity Data model in your EF Core project for Database First Approach.

Share This Post

Linkedin
Fb Share
Twitter Share
Reddit Share

Support Me

Buy Me A Coffee