ReadonlyDbContextGenerator 0.0.1

dotnet add package ReadonlyDbContextGenerator --version 0.0.1                
NuGet\Install-Package ReadonlyDbContextGenerator -Version 0.0.1                
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.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReadonlyDbContextGenerator --version 0.0.1                
#r "nuget: ReadonlyDbContextGenerator, 0.0.1"                
#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 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                

Stand With Ukraine

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

  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 ReadOnlyEfCoreGenerator
    
  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();
    

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; }
}
There are no supported framework assets in this 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
0.0.1 58 12/21/2024