Excluding tables in Entity Framework Core Scaffold-DbContext command

In Entity Framework Core DB First if you don’t want models created for certain tables you need to explicitly list the tables you do want models created for in the Scaffold-DbContext command. There is no tables to exclude parameter. If you have a large number of tables and only want to exclude a few this is tedious.

Microsoft have an issue for this on their backlog, but it doesn’t look like its going to get into EF Core 5 (November 2020) so to ease your pain you can…

Wrap the Scaffold-DbContext command with your long list of tables in a .bat file which helps with rerunnability so you don’t have to keep on specifying your table names

or a much nicer option is to

Switch from command line to UI based scaffolding using EF Core Power Tools which is a great Visual Studio extension which will allow you to just unselect the tables you don’t need when scaffolding. Thankfully it can also remember your selection for next time too.

Select tables to include/exclude

Leave a Reply

Your email address will not be published. Required fields are marked *