.NET Daily

How to use DbContext Scaffold to add only selected tables – ASP.NET Core

Introduction

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.


LATEST POSTS

How to solve: An expression tree may not contain a dynamic operation – ASP.NET Core 06th July, 2019

How to Prevent Visual Studio 2019 to Open a New Browser Instance when Debugging 06th June, 2019

Generic

How to use DbContext Scaffold to add only selected tables – ASP.NET Core

Posted on .

In the situation when you develop your database gradually along with your ASP.NET Core project, you will end up in the situation where you need to add additional entities to your application, other than the ones you developed in day 1.

Now, in order not to loose all the changes that you done to the existing models, like validation decorations, you have the option to use the -t parameter when doing scaffolding.

An example of a command that it is updating the Models only with the selected tables can be seen below:

Scaffold-DbContext "Data Source=NOTEBOOK\SQLEXPRESS;Initial Catalog=Articles;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t ArticleType, Article -f

Now, there is a caveat: When you execute the above command, the DBContext class, will be rewritten and it will contain only the newly added entities.

However, as a workaround, you can do first a backup of the file, and after the new file is generated, you can add in the code for the new entities.

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

There are no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

View Comments (0) ...
Navigation