ReadonlyDbContextGenerator 0.0.2
See the version list below for details.
dotnet add package ReadonlyDbContextGenerator --version 0.0.2
NuGet\Install-Package ReadonlyDbContextGenerator -Version 0.0.2
<PackageReference Include="ReadonlyDbContextGenerator" Version="0.0.2" />
paket add ReadonlyDbContextGenerator --version 0.0.2
#r "nuget: ReadonlyDbContextGenerator, 0.0.2"
// Install ReadonlyDbContextGenerator as a Cake Addin #addin nuget:?package=ReadonlyDbContextGenerator&version=0.0.2 // Install ReadonlyDbContextGenerator as a Cake Tool #tool nuget:?package=ReadonlyDbContextGenerator&version=0.0.2
ReadOnly DbContext Source Generator
Overview
The ReadOnlyDbContextGenerator
is a C# source generator that creates read-only versions of EF Core DbContext and entities.
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
IEntityTypeConfiguration
implementations. - 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.
IReadOnlyDbContext
interface.
- Produces source files for:
Usage
Add the source generator to your project:
dotnet add package ReadonlyDbContextGenerator
Build 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();
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 : IReadOnlyMyDbContext
{
public DbSet<ReadOnlyUser> Users { get; }
public DbSet<ReadOnlyOrder> Orders { get; }
public int SaveChanges()
{
throw new NotImplementedException("Read-only context");
}
}
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
{
DbSet<ReadOnlyUser> Users { get; }
DbSet<ReadOnlyOrder> Orders { get; }
DbSet<TEntity> Set<TEntity>() where TEntity : class;
}
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.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.10.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.