ReadonlyDbContextGenerator 0.0.1
dotnet add package ReadonlyDbContextGenerator --version 0.0.1
NuGet\Install-Package ReadonlyDbContextGenerator -Version 0.0.1
<PackageReference Include="ReadonlyDbContextGenerator" Version="0.0.1" />
paket add ReadonlyDbContextGenerator --version 0.0.1
#r "nuget: ReadonlyDbContextGenerator, 0.0.1"
// Install ReadonlyDbContextGenerator as a Cake Addin #addin nuget:?package=ReadonlyDbContextGenerator&version=0.0.1 // Install ReadonlyDbContextGenerator as a Cake Tool #tool nuget:?package=ReadonlyDbContextGenerator&version=0.0.1
ReadOnly DbContext Source Generator
Overview
The ReadOnlyDbContextGenerator
is a C# source generator that creates read-only versions of EF Core DbContext and entities. It ensures that the generated DbContext and entities prevent modifications, making them suitable for read-only operations in applications.
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 ReadOnlyEfCoreGenerator
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 class ReadOnlyMyDbContext : IReadOnlyMyDbContext
{
public IReadOnlyCollection<User> Users { get; }
public IReadOnlyCollection<Order> 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 interface IReadOnlyMyDbContext : IDisposable, IAsyncDisposable
{
IReadOnlyCollection<ReadOnlyUser> Users { get; }
IReadOnlyCollection<ReadOnlyOrder> Orders { get; }
}
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.
Version | Downloads | Last updated |
---|---|---|
0.0.1 | 58 | 12/21/2024 |