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.