JsonDatabaseContext 1.0.0

dotnet add package JsonDatabaseContext --version 1.0.0
                    
NuGet\Install-Package JsonDatabaseContext -Version 1.0.0
                    
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="JsonDatabaseContext" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonDatabaseContext" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="JsonDatabaseContext" />
                    
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 JsonDatabaseContext --version 1.0.0
                    
#r "nuget: JsonDatabaseContext, 1.0.0"
                    
#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 JsonDatabaseContext@1.0.0
                    
#: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=JsonDatabaseContext&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=JsonDatabaseContext&version=1.0.0
                    
Install as a Cake Tool

JsonDatabaseContext

Introduction

JsonDatabaseContext is a lightweight implementation of a DbContext similar to Entity Framework, but using JSON files for persistent storage. It's ideal for small applications that need data persistence without requiring a full database system.

Key Features

  • JSON-based persistence: Stores data in human-readable JSON files
  • CRUD operations: Supports basic Create, Read, Update, and Delete operations
  • Thread-safe: Uses semaphores to ensure thread-safe access
  • Easy integration: Simple to set up and use in .NET projects

Setup

1. Add Configuration

In your appsettings.json, add the database configuration:

json
{
  "DbContextOptions": {
    "BasePath": "Data/JsonDatabase"
  }
}

2. Register the Service

In your dependency configuration file (typically Program.cs):

builder.Services.Configure<DbContextOptions>(builder.Configuration.GetSection(DbContextOptions.SectionKey));
builder.Services.AddSingleton<IJsonDbContext, JsonDbContext>();     //or AddScoped

Basic Usage

Getting a DbSet

To work with an entity, first get its JsonDbSet:

var dbSet = await _dbContext.SetAsync<MyEntity>();

CRUD Operations

Create an Entity

var newEntity = new MyEntity { /* properties */ };
await dbSet.AddAsync(newEntity);

Read Entities

// All entities
var all = dbSet.GetAll();

// With filter
var filtered = dbSet.GetAll(e => e.Property == value);
Update an Entity
csharp
var entity = dbSet.GetAll().First();
entity.Property = newValue;
await dbSet.UpdateAsync(entity);
Delete an Entity
csharp
var entity = dbSet.GetAll().First();
await dbSet.RemoveAsync(entity);

Entity Requirements

All entities must implement the IJsonEntity interface:

public class MyEntity : IJsonEntity
{
    public Guid Id { get; set; }
    // other properties...
}

File Structure

Data is stored in JSON files at the configured path (BasePath), with one file per entity type:

Data/
  JsonDatabase/
    MyEntity.json
    AnotherEntity.json

Considerations

  • Performance: This approach is suitable for small datasets. For large-scale data, consider a traditional database.
  • Concurrency: While thread-safe, heavy concurrent access may impact performance.
  • Migrations: No migration system included. Entity changes may require manual JSON file manipulation.

Complete Example

// Entity definition
public class Product : IJsonEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Service usage
public class ProductService
{
    private readonly IJsonDbContext _dbContext;
    
    public ProductService(IJsonDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    
    public async Task AddProduct(string name, decimal price)
    {
        var products = await _dbContext.SetAsync<Product>();
        await products.AddAsync(new Product {
            Id = Guid.NewGuid(),
            Name = name,
            Price = price
        });
    }
    
    public async Task<List<Product>> GetExpensiveProducts(decimal minPrice)
    {
        var products = await _dbContext.SetAsync<Product>();
        return products.GetAll(p => p.Price >= minPrice);
    }
}

License

[MIT License] - Free to use in your projects.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in 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
1.0.0 121 2/9/2026

Initial release of JsonDatabaseContext with core functionality for JSON-based data persistence.