SpocR 4.1.35

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet tool install --global SpocR --version 4.1.35
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local SpocR --version 4.1.35
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=SpocR&version=4.1.35
                    
nuke :add-package SpocR --version 4.1.35
                    

SpocR Publish NuGet NuGet Badge

  • Scaffolds your Stored Procedures and Models to C# Files
  • Easily managed through a CLI interface
  • Scalable and extensible architecture
  • No rigid dependencies for maximum flexibility

How SpocR works

SpocR extracts your database schema via a provided ConnectionString and stores it in a spocr.json configuration file. This configuration file is highly customizable, allowing you to select which schemas to include or exclude.

SpocR generates a complete DataContext folder structure with all required C# code for your .NET application (App, API, or Services).

The tool is designed for flexibility. You can:

  • Build it as a standalone project (Default mode)
  • Use it as a library to integrate into other projects (Library mode)
  • Create extensions to enhance existing SpocR libraries (Extension mode)

SpocR supports User-Defined Table Functions and various parameter types. The results of your Stored Procedures will be automatically mapped to strongly-typed models or as List<Model>. It also supports pure JSON-string results from Stored Procedures without building additional model classes.

Generated Folder Structure

./DataContext/
  ├── Models/[schema]/[StoredProcedureName].cs
  ├── StoredProcedures/[schema]/[EntityName]Extensions.cs
  ├── TableTypes/[schema]/[TableTypeName].cs
  ├── AppDbContext.cs
  ├── AppDbContextExtensions.cs
  ├── SqlDataReaderExtensions.cs
  └── SqlParameterExtensions.cs

Using the generated SpocR code

Step 1: Register the context

Register IAppDbContext in your application's dependency injection container:

// .NET 6+ in Program.cs
builder.Services.AddAppDbContext();

// Or in Startup.cs for older versions
services.AddAppDbContext();

Step 2: Inject the context

Inject IAppDbContext into your business logic components:

private readonly IAppDbContext _dbContext;

public MyManager(IAppDbContext dbContext)
{
    _dbContext = dbContext;
}

Step 3: Call stored procedures

Use the generated extension methods to call your stored procedures:

public Task<List<UserList>> ListAsync(CancellationToken cancellationToken = default)
{
    return _dbContext.UserListAsync(User.Id, cancellationToken);
}

Naming Conventions

StoredProcedure Naming Pattern

[EntityName][Action][Suffix]
  • EntityName (required): Name of the base SQL table
  • Action (required): Create | Update | Delete | (Merge, Upsert) | Find | List
  • Suffix (optional): WithChildren | [custom suffix]

Required Result Format for CRUD Operations

For Create, Update, Delete, Merge, and Upsert operations, stored procedures should return:

  • [ResultId] INT: Operation result status
  • [RecordId] INT: ID of the affected record

Technical Requirements

  • Database: SQL Server version 2012 or higher
  • Framework: .NET Core / .NET 6+ (supports down to .NET Core 2.1)
  • Current Version: 4.0.0 (as of April 2025)

Required .NET Packages

  • Microsoft.Data.SqlClient
  • Microsoft.Extensions.Configuration

Installation Guide

First, ensure you have the .NET SDK installed (latest version recommended)

dotnet tool install --global SpocR

Option B: Install from GitHub Source

# Clone the repository
git clone https://github.com/nuetzliches/spocr.git

# Uninstall previous versions if needed
dotnet tool uninstall -g spocr

# Build and install from source
cd src
(dotnet msbuild -t:IncrementVersion)
dotnet pack --output ./ --configuration Release
dotnet tool install -g spocr --add-source ./

Using SpocR

Quick Start

To quickly set up your project:

# Create and configure spocr.json
spocr create

# Pull schemas and build DataContext
spocr rebuild

Step-by-Step Approach

If you prefer more control:

# Step 1: Pull database schemas and update spocr.json
spocr pull

# Step 2: Build DataContext folder
spocr build

Removing SpocR

To remove SpocR configuration and/or generated code:

spocr remove

Advanced Configuration

Project Role Types in spocr.json

Project.Role.Kind

  • Default: Creates a standalone project with all dependencies
  • Lib: Creates a SpocR library for integration into other projects, including AppDbContext and dependencies
  • Extension: Creates an extensible project without AppDbContext and dependencies to extend an existing SpocR library. Requires configuring the namespace (Project.Role.LibNamespace) to resolve the SpocR library

Sample Implementation

For a complete example project with stored procedures and API implementation, visit: https://github.com/nuetzliches/nuts

Additional Resources

Known Issues and Limitations

  • SQL Server cannot reliably determine the nullable property for computed columns. For cleaner models, wrap computed columns in ISNULL({computed_expression}, 0) expressions.
  • When using complex types as parameters, ensure they follow the required table type structure.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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 was computed.  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.

This package has no dependencies.

Version Downloads Last Updated
4.5.0-alpha.12 153 10/7/2025
4.1.35 241 8/4/2025
4.1.34 208 8/4/2025
4.1.33 197 8/4/2025
4.1.32 172 6/6/2025
4.1.31 185 6/5/2025
4.1.30 236 4/29/2025
4.1.29 233 4/29/2025
4.1.28 222 4/29/2025
4.1.27 251 4/24/2025
4.1.26 233 4/24/2025
4.1.25 247 4/24/2025
4.1.24 217 4/23/2025
4.1.23 226 4/23/2025
4.1.22 226 4/23/2025
4.1.21 213 4/23/2025
4.1.20 229 4/23/2025
4.1.19 241 4/23/2025
4.1.18 223 4/23/2025
4.1.17 223 4/23/2025
Loading failed