Entity Framework DB First quick start using EF Core Power Tools

It’s simple enough to get started with the Entity Framework Database First approach using the command line, but it’s even easier using the Visual Studio EF Core Power Tools extension.

In this post we’ll …

  • Create a simple DB (but use an existing one if you want).
  • Reverse engineer it into C# models using EF Core Power Tools.
  • We’ll also turn on logging to the Debug window.
  • Finally we’ll run a simple query.

Create a sample DB

Create a sample DB and dummy data using this SQL script which based on a Stack Overflow site structure.

The script should take about 20 seconds to execute and you’ll end up with the below tables which will be populated with dummy records …

Install EF Core Power Tools extension in Visual Studio

Install the EF Core Power Tools extension into Visual Studio from the Extensions -> Manage Extensions menu.

After you click Download, the extension is scheduled for install and will be installed after all instances of Visual Studio have been closed and reopened.

Create a new .NET 7 console app

Create a new .NET 7 console app or website.
In this post I’ve just created a console app for simplicity.

Reverse Engineer Database

Right click on the new project and select EF Core Power Tools -> Reverse Engineer

In the Choose Your Data Connection screen select select EF Core 7 and click Add Database Connection.

In the Connection Properties screen select the server and DB where you created the example DB above …

Click OK on the above screen and on the Choose your Data Connection screen to move to the Choose Your Database Objects screen below.

Select all tables ….

Next we’ll go to the Choose Your Settings for Project XYZ screen …

Leave most of the defaults, just check the highlighted options above.

The first option will include the connection string directly in the OnConfiguring method of the generated DbContext. This is fine for this quick start but ideally this will come from an appSettings file or from Azure Key Vault for example.

After you click OK, EF Core Power Tools will generate models, a DbContext and an EF Core Power Tools settings file.

The solution structure should now look a bit like …

Turn on logging to to Debug window

Before we send a simple query let’s turn on logging so that queries which Entity Framework sends to the DB get written to the debug window.

We can do this by modifying the DbContext OnConfiguring method …

DbContext customizations will be lost when we re-scaffold the DB

Note, when we are using the Entity Framework Database First approach and we manually change the generated OnConfiguring method like above (or anything in the DbContext) and re-reverse engineer the DB, we will lose our customizations.

To avoid this we can subclass the generated Entity Framework DbContext, make our customizations in the non auto-generated subclass and use the subclass in our app.

Run a simple query

Go to your Program.cs file and start querying …

20 usernames output to the console …

… and if we wish we can see the command that Entity Framework has sent to the Database in the Debug output window …

We are done!

Please drop any questions about this Entity Framework DB First quick start example into the comments section below.

4 thoughts on “Entity Framework DB First quick start using EF Core Power Tools”

  1. Great post! I have to say, i have missed the autogeneration capabilities of early EF versions in Visual Studio. Such a huge time saver, so these power tools are very welcome.

    Instead of subclassing to protect our customized code from being wiped out through re-generation, why not use partial classes? That’s generally how .NET has protected user customization in the past.

  2. Great post, thanks! I’m trying to jump on board EF, so appologize for the beginner’s question. Suppose I add a new column on the Users table using SSMS. What would I need to do to have it available in the dbcontext?

    1. Thanks Sergio. Simply add the column and repeat the process above (eg. – re-reverse engineer again like above). The tool will update your dbcontext and the Users model automatically.

Leave a Reply

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