ReadonlyDbContextGenerator 0.2.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReadonlyDbContextGenerator --version 0.2.4
                    
NuGet\Install-Package ReadonlyDbContextGenerator -Version 0.2.4
                    
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="ReadonlyDbContextGenerator" Version="0.2.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ReadonlyDbContextGenerator" Version="0.2.4" />
                    
Directory.Packages.props
<PackageReference Include="ReadonlyDbContextGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ReadonlyDbContextGenerator --version 0.2.4
                    
#r "nuget: ReadonlyDbContextGenerator, 0.2.4"
                    
#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.
#:package ReadonlyDbContextGenerator@0.2.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ReadonlyDbContextGenerator&version=0.2.4
                    
Install as a Cake Addin
#tool nuget:?package=ReadonlyDbContextGenerator&version=0.2.4
                    
Install as a Cake Tool

Stand With Ukraine

ReadOnly DbContext Source Generator

NuGet version (ReadOnlyDbContextGenerator)

Overview

The ReadOnlyDbContextGenerator is a C# source generator that creates read-only versions of EF Core DbContext and entities.

See CHANGELOG.md for release notes and recent changes.

How It Works

  1. DbContext Analysis

    • Identifies classes inheriting from Microsoft.EntityFrameworkCore.DbContext.
    • Extracts the DbSet properties and their entity types.
  2. Entity Processing

    • Analyzes the entity types referenced in the DbSet properties.
    • Converts their properties to init-only.
    • Modifies navigation properties to reference read-only entities or collections.
  3. Entity Configuration

    • Identifies and processes IEntityTypeConfiguration implementations.
    • Generates read-only configuration classes for the entities.
  4. Code Generation

    • Produces source files for:
      • Read-only DbContext.
      • Read-only entity classes.
      • Entity configuration classes.
      • IReadOnlyDbContext interface.

Usage

  1. Add the source generator to your project:

    dotnet add package ReadonlyDbContextGenerator
    
  2. Build your project. The generator will create source files for the read-only components.

  3. Access the generated read-only DbContext and entities:

    var readOnlyDbContext = new ReadOnlyMyDbContext();
    

Read-only contract

Generated contexts inherit from the original DbContext type and throw NotSupportedException from every SaveChanges and SaveChangesAsync overload. Generated entities expose init accessors, and generated interfaces expose queryable sets rather than mutable DbSet members.

This is an application-level guard. EF Core bulk operations such as ExecuteUpdate and ExecuteDelete run directly against the database and do not call SaveChanges. Use database credentials with read-only permissions when writes must be prevented reliably.

The generator supports entities declared in the consuming project, navigation collections, owned types configured with OwnsOne, OwnsMany, or Owned, record entities, and IEntityTypeConfiguration<T> implementations. DbSet properties whose entity type is defined in another assembly are skipped with diagnostic RDCTX001.

Example

Input

DbContext
public class MyDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Order> Orders { get; set; }
}
Entity
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public string Product { get; set; }
    public User User { get; set; }
}

Generated Output

ReadOnlyMyDbContext
public partial class ReadOnlyMyDbContext : DbContext, IReadOnlyMyDbContext
{
    public DbSet<ReadOnlyUser> Users { get; }
    public DbSet<ReadOnlyOrder> Orders { get; }

    public sealed override int SaveChanges()
    {
        throw new NotSupportedException("Saving changes is not supported by a readonly db context.");
    }

    public sealed override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        throw new NotSupportedException("Saving changes is not supported by a readonly db context.");
    }

    IQueryable<ReadOnlyUser> IReadOnlyMyDbContext.Users => Users;
    IQueryable<ReadOnlyOrder> IReadOnlyMyDbContext.Orders => Orders;
    IQueryable<TEntity> IReadOnlyMyDbContext.Set<TEntity>()
        where TEntity : class => Set<TEntity>();
}
ReadOnlyUser
public class ReadOnlyUser
{
    public int Id { get; init; }
    public string Name { get; init; }
    public IReadOnlyCollection<ReadOnlyOrder> Orders { get; init; }
}
ReadOnlyOrder
public class ReadOnlyOrder
{
    public int Id { get; init; }
    public string Product { get; init; }
    public ReadOnlyUser User { get; init; }
}
IReadOnlyMyDbContext
public partial interface IReadOnlyMyDbContext : IDisposable, IAsyncDisposable
{
    IQueryable<ReadOnlyUser> Users { get; }
    IQueryable<ReadOnlyOrder> Orders { get; }
    IQueryable<TEntity> Set<TEntity>() where TEntity : class;
    DatabaseFacade Database { get; }
}

Contributing

Contributions are welcome! Please submit issues or pull requests on the ReadonlyDbContextGenerator.

We welcome ideas, bug reports, and feature suggestions from the community!

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
0.2.6 97 10/13/2026
0.2.5 76 10/13/2026
0.2.4 224 9/22/2026
0.2.3 248 9/11/2026
0.2.2 279 9/7/2026
0.2.1 287 9/7/2026
0.2.0 290 9/6/2026
0.1.7 1,712 3/5/2026
0.1.6 390 3/2/2026
0.1.5 742 1/26/2026
0.1.4 722 1/4/2026
0.1.3 334 12/16/2025
0.1.2 744 12/2/2025
0.1.1 258 11/24/2025
0.1.0 313 6/5/2025
0.0.9 271 3/28/2025
0.0.8 620 3/25/2025
0.0.7 562 3/25/2025
0.0.6 252 3/20/2025
0.0.5 203 1/11/2025
Loading failed