ReadonlyDbContextGenerator 0.2.5
See the version list below for details.
dotnet add package ReadonlyDbContextGenerator --version 0.2.5
NuGet\Install-Package ReadonlyDbContextGenerator -Version 0.2.5
<PackageReference Include="ReadonlyDbContextGenerator" Version="0.2.5"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ReadonlyDbContextGenerator" Version="0.2.5" />
<PackageReference Include="ReadonlyDbContextGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ReadonlyDbContextGenerator --version 0.2.5
#r "nuget: ReadonlyDbContextGenerator, 0.2.5"
#:package ReadonlyDbContextGenerator@0.2.5
#addin nuget:?package=ReadonlyDbContextGenerator&version=0.2.5
#tool nuget:?package=ReadonlyDbContextGenerator&version=0.2.5
ReadOnly DbContext Source Generator
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
DbContext Analysis
- Identifies classes inheriting from
Microsoft.EntityFrameworkCore.DbContext. - Extracts the DbSet properties and their entity types.
- Identifies classes inheriting from
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.
Entity Configuration
- Identifies and processes
IEntityTypeConfigurationimplementations. - Generates read-only configuration classes for the entities.
- Identifies and processes
Code Generation
- Produces source files for:
- Read-only DbContext.
- Read-only entity classes.
- Entity configuration classes.
IReadOnlyDbContextinterface.
- Produces source files for:
Usage
Add the source generator to your project:
dotnet add package ReadonlyDbContextGeneratorBuild your project. The generator will create source files for the read-only components.
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!
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 |