Reverse Engineer a SQL-Server database with EF Power Tools
Note
Before starting, EF Power tool does not support EF Core 5, only EF Core 6 and EF Core 7.
In this article learn how to create a database with three tables in SSMS (SQL-Server Management Studio) followed by creating POCO classes which will represent tables in the new database.
Once the POCO classes 1 have been created with EF Power Tools 2 we will write a few queries.
You will need Microsoft Visual Studio 2022 or higher and Microsoft SSMS.
Create the database
- Open SSMS
- Open a connection to
(localdb)\MSSQLLocalDB
- From
Object Explorer
, right click onDatabases
- Select
New database
- Enter
NorthWindContacts
for the database name, click OK - Right click on
NorthWindContacts
, selectNew query
- Open NorthWindContacts.sql, copy the contents
- Back in SSMS, paste the contents into the new query
- Click
Execute
- Right click on
NorthWindContacts
and selectRefesh
, there should be three tables.
Install EF Core Power Tools
- With Visual Studio closes, download EF Power Tools from the following link.
- Double click on the download, follow the prompts
Open Visual Studio to this solution
- Right click on the top node in Solution Explorer
- Select create a new Console project (check do not use top down statements) named NorthWindContactsApp
- Right click on the project name in Solution Explorer, select
Manage NuGet packages
- On the Browse tab, paste in
Microsoft.EntityFrameworkCore.SqlServer
, clickInstall
or use the NuGet command promptInstall-Package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.2
- Right click on the project name in Solution Explorer
- Select
EF Power Tools
- Select
Reverse Engineer
- Configure a new database connection (see figure 1) and version of EF Core
- Set connection properties (see figure 2)
- Accept (see figue 3)
- Select tables (see figure 4)
- Next, set several settings (see figure 5)
-
Context name:
NorthWindContext
-
Namespace:
NorthWindContactsApp
-
EntityTypes:
Models
-
DbCotext path:
Data
-
Check
Include connection string
in generated code (okay for learning but should not be done for a real app)
-
Context name:
Figure 1
Figure 2
Figure 3
Figure 4
Add another package to the project
Add the NuGet package Spectre.Console to the project
Write code to get data
Open Program.cs and replace the contents with the following which will select the top 20 contacts into a table shown below
using Microsoft.EntityFrameworkCore;
using NorthWindContactsApp.Data;
using Spectre.Console;
namespace NorthWindContactsApp
{
internal class Program
{
static void Main(string[] args)
{
using var context = new NorthWindContext();
var contacts = context.Contacts
.Include(x => x.ContactTypeIdentifierNavigation)
.Include(x => x.ContactDevices)
.Take(20)
.ToList();
var table = CreateTable();
foreach (var contact in contacts)
{
if (contact.ContactDevices.Any())
{
table.AddRow(contact.ContactId.ToString(),
contact.FirstName,
contact.LastName,
contact.ContactTypeIdentifierNavigation.ContactTitle,
contact.ContactDevices.FirstOrDefault()!.PhoneNumber);
}
else
{
table.AddRow(contact.ContactId.ToString(),
contact.FirstName,
contact.LastName,
contact.ContactTypeIdentifierNavigation.ContactTitle,
"[red](none)[/]");
}
}
AnsiConsole.Write(table);
Console.ReadLine();
}
private static Table CreateTable()
{
return new Table()
.RoundedBorder().BorderColor(Color.LightSlateGrey)
.AddColumn("[b]Id[/]")
.AddColumn("[b]First[/]")
.AddColumn("[b]Last[/]")
.AddColumn("[b]Title[/]")
.AddColumn("[b]Phone[/]")
.Alignment(Justify.Center)
.Title("[white on blue]Contacts[/]");
}
}
}
Run the project and the following shows the top 20 contacts
Connection string
In the above instructions the connection string is hard coded into the DbContext. When working say with ASP.NET Core the connection string is moved to the appsetting.json
file under a section ConnectionStrings.
Then to get the connection string we have code such as the following will is not showing configuration
as each developer may do this differently e.g. use hard coded path or environment variables.
builder.Services.AddDbContextPool<Context>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
Next steps
- Study the code to get an understanding how things work
- Read Microsoft docs on EF Core
- Remove the connection string from
NorthWindContext.cs
and place intoappsettings.json
. Look at the projectModelCommentsApp
,appsettings.json
has the connection string. A NuGet package, ConfigurationLibrary is used to set the connection string inBookContext.cs
inOnConfiguring
and note there are several options to log or not log EF Core operations.
Note In the above steps we defaulted to setting the database connection string in the DDbContext. Take time to learn how to place the connection string into a configuration file such as appsettings.json. This was not gone over as the focus is how to use EF Power Tools.
Learn more about EF Power Tools
📖 See Erikj's Wiki
You also might like
-
A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal .NET CLR class, which is why it is called "Plain Old CLR Objects". ↩
-
Useful design-time DbContext features, added to the Visual Studio Solution Explorer context menu. When right-clicking on a C# project, the following context menu functions are available: Reverse Engineer - Generates POCO classes. ↩