Skip to content

AKOvinno/MiniOrm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Projects

Project Purpose
MiniOrm/ Core ORM library + demo Program.cs
MiniOrm.Migrations/ CLI migration tool

Setup

1. Create the database in Postgres

CREATE DATABASE miniorm;

Note: Database Username=postgres and Password=root

2. Set the connection string

export MINIORM_CONN="Host=localhost;Database=miniorm;Username=postgres;Password=root"

How to run the migrations

cd MiniOrmMigration

# Generate migration SQL file
dotnet run -- migrations add InitialCreate

# Review the generated file in Migrations/
# Apply all pending migrations
dotnet run -- migrations apply

# List applied / pending
dotnet run -- migrations list

# Rollback the last applied migration
dotnet run -- migrations rollback

How to run the demo

cd MiniOrm
dotnet run

The demo:

  1. Connects to PostgreSQL
  2. Inserts two Product rows (one with Discount = NULL)
  3. Finds a row by id
  4. Updates price and discount
  5. Lists all rows
  6. Deletes all rows

Attribute Filtering

We have created the Product entity and we want the products table to be created in the database. When we run migrations add, MiniOrm scans the Product class and looks at every property one by one. But not every property should become a database column — for example a navigation property like public List<Order> Orders should never become a column. So MiniOrm checks each property for a [Column] or [PrimaryKey] attribute. If a property has neither of these attributes, it is silently skipped and ignored completely. If it has [PrimaryKey], it is marked as the primary key column. If it has [Column("name")], the name inside the attribute becomes the column name in the database. This filtering process is called attribute filtering — it is the thing that decides which properties are allowed into the database and which are not.


Type Mapping

Once attribute filtering is done, MiniOrm has a list of properties that should become columns. But now it needs to know what SQL type to give each column. So for each filtered property, MiniOrm checks its C# type and translates it into the correct PostgreSQL equivalent. If the property is the primary key, it becomes SERIAL PRIMARY KEY. If the property is decimal?, the ? tells MiniOrm it is nullable, so it becomes NUMERIC NULL. If it is decimal without the ?, it becomes NUMERIC NOT NULL. If it is string? it becomes TEXT NULL, if it is string it becomes TEXT NOT NULL. Every C# type has a corresponding PostgreSQL type. This translation process is called type mapping — it is the translator that converts C# language into PostgreSQL language so the database can understand what kind of data each column will hold.


About

A lightweight ORM built from scratch in C# using ADO.NET and Npgsql, featuring reflection-based entity mapping, a generic DbSet with full CRUD, and a command-line migration CLI targeting PostgreSQL

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages