NiORM 1.5.0

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

NiORM

NiORM is a lightweight Object-Relational Mapper (ORM) for .NET, designed to simplify interactions with SQL databases. It uses a convention-over-configuration approach, enabling developers to map C# classes to database tables using attributes. NiORM offers an intuitive interface for querying and manipulating data with minimal overhead.

Features

  • Attribute-based Mapping: Use [TableName], [PrimaryKey], and other annotations to define the schema directly in your C# classes.
  • Entity Management: Create, retrieve, update, and delete records easily through Entities<T> collections.
  • Query Simplification: Chain LINQ-style queries for simple and advanced data filtering.
  • Raw SQL Execution: Execute raw SQL queries when needed, returning mapped objects.
  • Multiple Database Support: Handle multiple databases within the same project.
  • πŸ” SQL Injection Protection: All methods use parameterized queries by default for maximum security.
  • πŸ†• Security Validation: Automatic detection and prevention of SQL injection attacks.
  • πŸ†• Comprehensive Error Handling: Custom exception classes for better error identification and handling.
  • πŸ†• Built-in Logging System: Configurable logging with multiple levels and output options.
  • πŸ†• Enhanced XML Documentation: Complete IntelliSense support with detailed examples and exception documentation.

Installation

Download & Install the nuget using:

Nuget Package Manager:

NuGet\Install-Package NiORM -Version

.Net CLI:

dotnet add package NiORM

Quick Start

Here’s how you can get started with NiORM in your application:

1. Define a Model:

Use attributes like [TableName] and [PrimaryKey] to map a C# class to a database table.

using NiORM.Attributes;
using NiORM.SQLServer.Interfaces;

[TableName("People")]
public class Person : ITable
{
    [PrimaryKey(isAutoIncremental: true)]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
2. Set up a Data Service:

Create a service class that inherits from DataCore. This class will act as the interface between your application and the database.

using NiORM.SQLServer;
using NiORM.Test.Models;

public class DataService : DataCore
{
    public DataService(string connectionString) : base(connectionString) { }

    public IEntities<Person> People => CreateEntity<Person>();
}
3. Interact with the Database (SQL Injection Safe):

Use the data service to fetch, insert, update, and delete records. All operations are protected against SQL injection!

var dataService = new DataService("your-connection-string-here");

// Fetch all people
var people = dataService.People.ToList();

// Add a new person (safe!)
var newPerson = new Person() { Age = 29, Name = "John'; DROP TABLE Users; --" };
dataService.People.Add(newPerson); // This is completely safe!

// Safe queries with parameterized WHERE conditions
var filteredPeople = dataService.People.Where(p => p.Name == "Nima").ToList();

// Safe multiple conditions
var conditions = new Dictionary<string, object?>
{
    { "Name", "John'; DROP TABLE Users; --" }, // Safe!
    { "Age", 25 }
};
var safePeople = dataService.People.WhereMultiple(conditions);

// Safe property search
var byName = dataService.People.FindByProperty("Name", userInput); // Always safe!
4. Execute Raw SQL (when necessary - with security warnings):

If you need more control, you can execute raw SQL queries. NiORM will automatically validate and warn about potential security issues.

// ⚠️ Raw SQL (automatically validated for injection attempts)
var cats = dataService.SqlRaw<Cat>("SELECT * FROM Cats");

// βœ… Better: Use safe parameterized approach
var paramHelper = new SqlParameterHelper();
var nameParam = paramHelper.AddParameter("Fluffy");
var safeCats = dataService.SqlRaw<Cat>($"SELECT * FROM Cats WHERE Name = {nameParam}");

🚨 Important: Always prefer safe methods like FindByProperty(), WhereMultiple(), and LINQ expressions over raw SQL!

πŸ” Security & Error Handling

NiORM v1.5.0+ includes comprehensive SQL injection protection, error handling, and security logging:

Enable Logging:
using NiORM.SQLServer.Core;

// Enable logging to console
NiORMLogger.IsEnabled = true;
NiORMLogger.MinimumLogLevel = LogLevel.Info;

// Or log to file
NiORMLogger.LogFilePath = @"C:\logs\niorm.log";
Enhanced Error Handling:
try
{
    var person = dataService.People.Find(123);
}
catch (NiORMValidationException ex)
{
    // Handle validation errors
    Console.WriteLine($"Validation error: {ex.Message}");
}
catch (NiORMConnectionException ex)
{
    // Handle connection issues
    Console.WriteLine($"Connection failed: {ex.Message}");
}
catch (NiORMException ex)
{
    // Handle other NiORM errors
    Console.WriteLine($"Database error: {ex.Message}");
    if (!string.IsNullOrEmpty(ex.SqlQuery))
    {
        Console.WriteLine($"Failed query: {ex.SqlQuery}");
    }
}
Safe Operations with Enhanced Service:
public class SafeDataService : DataCore
{
    public SafeDataService(string connectionString) : base(connectionString)
    {
        // Configure logging
        NiORMLogger.IsEnabled = true;
        NiORMLogger.MinimumLogLevel = LogLevel.Warning;
    }

    public bool TryAddPerson(Person person, out string errorMessage)
    {
        errorMessage = string.Empty;
        try
        {
            People.Add(person);
            return true;
        }
        catch (NiORMException ex)
        {
            errorMessage = ex.Message;
            return false;
        }
    }

    public IEntities<Person> People => CreateEntity<Person>();
}

πŸ“– For detailed documentation, see:

Contributing

We welcome contributions! Please fork the repository and submit pull requests for any improvements or features you'd like to add.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  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.

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.5.0 140 6/29/2025
1.4.0 134 6/29/2025
1.3.14 154 2/10/2025
1.3.13 145 11/17/2024
1.3.12 118 11/16/2024
1.3.11 129 10/14/2024
1.3.10 137 10/2/2024
1.3.9 135 10/2/2024
1.3.8 134 9/29/2024
1.3.7 131 9/29/2024
1.3.6 125 9/29/2024
1.3.5 141 9/29/2024
1.3.4 133 9/26/2024
1.3.3 154 9/26/2024
1.3.2 152 9/23/2024
1.3.1 142 9/23/2024
1.2.10 292 8/22/2023
1.2.9 249 6/13/2023
1.2.8 200 5/29/2023
1.2.7 245 4/24/2023
1.2.6 242 4/20/2023
1.2.5 264 4/16/2023
1.2.4 248 4/16/2023
1.2.3 273 4/8/2023
1.2.2 274 4/8/2023
1.2.1 267 4/8/2023
1.1.6 252 4/5/2023
1.1.5 330 2/15/2023
1.1.4 319 2/15/2023
1.1.3 387 1/28/2023
1.1.2 366 1/28/2023
1.1.1 427 1/15/2023
1.0.2 367 1/9/2023
1.0.1 381 1/8/2023
1.0.0 374 1/8/2023

SECURITY UPDATE v1.5.0: Complete SQL injection protection with parameterized queries, automatic security validation, safe alternative methods (WhereMultiple, FindByProperty), security logging, and comprehensive protection guide. All existing methods now secure by default!