Skip to main content
net6.0

A configurable alternative to the `dotnet ef dbcontext scaffold` command for generating a DbContext and its entities

Published
Sep 28, 2023
3y ago
Total Downloads
3.2K
3,218

Install

dotnet add package EFCore.Scaffolding --version 1.0.0
ProductVersions
.NET
net6.0

README

EFCore.Scaffolding is a configurable alternative to the dotnet ef dbcontext scaffold command for generating a DbContext and its entities.

NuGet Continuous Integration Coverage

Getting started

Add the EFCore.Scaffolding NuGet package to your project using the NuGet Package Manager or run the following command:

dotnet add package EFCore.Scaffolding

Reverse engineering a database, also known as scaffolding, is usually performed with the dotnet ef dbcontext scaffold command. This command has a few shortcomings that this project aims to address.

EF Core providers support

The following EF Core providers are supported out of the box. Pull requests are welcome to support additional providers.

Scaffolding requires to use a typed connection string builder so that the scaffolder knows which provider to use.

using EFCore.Scaffolding;
using Npgsql;

var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
    Host = "localhost",
    Database = "postgres",
    Username = "postgres",
    Password = "postgres",
};
Scaffolder.Run(new ScaffolderSettings(connectionStringBuilder));

Filtering

Both tables/entities and columns/properties can be filtered programmatically with the FilterTable and FilterColumn predicates of the ScaffolderSettings object. Sometimes it's easier to exclude a few tables rather than explicitly list dozen of tables.

Renaming

Databases in the real world are not perfect and often table and/or column names are less than ideal. The RenameEntity and RenameProperty functions are here to tweak the names as appropriate.

Sorting properties

By default, properties are scaffolded in the same order as columns appear in the tables. If you prefer to keep them sorted alphabetically, it's possible by setting SortColumnsComparer = new ColumnNameComparer(). It's even possible to write your own comparer if you need to sort them in any other way.

And more…

The ScaffolderSettings class has a few more properties that will help you generate a perfect DbContext and its entities. All the public API is documented with XML comments.

Sample code

Here's how to scaffold a real PostgreSQL database using the EFCore.Scaffolding package. This demonstrates how to filter out the fax columns and rename Fulltext with proper FullText casing. This also demonstrates how the files can be saved relative to the current C# file.

using System;
using System.IO;
using System.Runtime.CompilerServices;
using EFCore.Scaffolding;
using Npgsql;

var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
    Host = "ep-square-wildflower-00220644.us-east-2.aws.neon.tech",
    Database = "chinook",
    Username = "AzureDiamond",
    Password = "correct horse battery staple",
};

var settings = new ScaffolderSettings(connectionStringBuilder)
{
    OutputDirectory = GetOutputDirectory(),
    FilterColumn = column => column.Name != "fax",
    RenameProperty = (propertyName, _) => propertyName.Replace("Fulltext", "FullText"),
};

Scaffolder.Run(settings);

Console.WriteLine($"âś… The database was scaffolded in {new Uri(settings.OutputDirectory.FullName)}");

return;

static DirectoryInfo GetOutputDirectory([CallerFilePath] string path = "")
    => new(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path)!, "..", "ChinookDatabase")));

You can actually try to run this code, I have hosted the Chinook sample database on the free Neon tier.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.