EFCore.Scaffolding 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EFCore.Scaffolding --version 1.0.0                
NuGet\Install-Package EFCore.Scaffolding -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCore.Scaffolding" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFCore.Scaffolding --version 1.0.0                
#r "nuget: EFCore.Scaffolding, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFCore.Scaffolding as a Cake Addin
#addin nuget:?package=EFCore.Scaffolding&version=1.0.0

// Install EFCore.Scaffolding as a Cake Tool
#tool nuget:?package=EFCore.Scaffolding&version=1.0.0                

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.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 66 11/12/2024
2.0.0 207 9/16/2024
2.0.0-rc.1 78 4/22/2024
1.0.0 277 9/28/2023