If you want to test little snippets of code in Visual Studio before having to change your code and having to launch and debug your app, check out the Interactive window via the View -> Other Windows -> C# Interactive menu option.
The C# Interactive window is a REPL Editor, i.e. Read-Evaluate-Print-Loop and can be useful for things like
– checking date formats (shown below) – declaring and testing methods – testing strings – interpolation, substring, replace, trim etc. – testing LINQ queries on declared collections
Very nice introduction to the interactive window on the Microsoft Visual Studio channel on YouTube
It’s often useful to have our entities inherit from a base class. This is easily done using EF Core Power Tools which is a Visual Studio extension that provides a graphical rather than command line based approach to scaffolding.
EF Core Power Tools uses the Scaffolding.Handlebars library to support the customization of code which is generated during the scaffolding process. You don’t need the VS extension to use the library but it’s a bit easier and EF Core Power Tools is a great extension anyway so I’ll use it in the post.
The steps are…
Install the extension via the Extensions -> Manage Extensions window (you’ll then see the menu as a right-click option on your projects).
Right click on the relevant project and select EF Core Power Tools -> Reverse Engineer and first select which tables you want to scaffold and then on the subsequent screen check the option for customizing the code.
After running the scaffolding you should see entities generated AND a CodeTemplates folder which contains all the handlebar template files for customizing entities and the DbContext.
To inherit from a base class just amend the Class template (Class.hbs) file as shown on the left below.
Re-scaffold again with right click -> EF Core Power Tools -> Reverse Engineer and all your entities should now inherit from the base class
Of course to have your entities implement an interface the process is identical.
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.
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.
If your working on a large solution with many projects in Visual Studio you can speed up VS load time by excluding the projects you don’t need loaded (perhaps because other developers are working on them) using a Solution Filter.
If we have the following normal (unfiltered) solution we can apply solution filters very easily.
Right click on any non relevant projects and select ‘Unload Project’.
Right click on the solution and click ‘Save As Solution Filter’.
Double click the solution filter file (.snlf) to open the filtered solution.
In the above example I’ve excluded two projects (ConsoleApp2 and UnitTestProject1). The markup in the .snlf file is simple as can be seen below.