Gurux.Service
3.0.2611.501
dotnet add package Gurux.Service --version 3.0.2611.501
NuGet\Install-Package Gurux.Service -Version 3.0.2611.501
<PackageReference Include="Gurux.Service" Version="3.0.2611.501" />
<PackageVersion Include="Gurux.Service" Version="3.0.2611.501" />
<PackageReference Include="Gurux.Service" />
paket add Gurux.Service --version 3.0.2611.501
#r "nuget: Gurux.Service, 3.0.2611.501"
#:package Gurux.Service@3.0.2611.501
#addin nuget:?package=Gurux.Service&version=3.0.2611.501
#tool nuget:?package=Gurux.Service&version=3.0.2611.501
Gurux ORM
Gurux ORM is an open-source object-relational mapper for C#, distributed as the Gurux.Service NuGet package. It maps C# models to database tables and uses lambda expressions to build queries across multiple database engines.
Part of the Gurux Device Framework.
✨ Features
- Unified ORM interface for multiple databases
- Automatic table creation and relation traversal
- See generated SQL in debug mode (hover on the argument)
- Lambda-expression query support
- Attribute-driven schema mapping
- Supports
1:1,1:N, andN:Nrelationships - Cached schema status through
GXSchemaManager, with concurrent scan coalescing and DDL invalidation
🗃️ Supported Databases
Installation
Install the ORM and an ADO.NET provider for your database. For the SQLite example below:
dotnet add package Gurux.Service
dotnet add package System.Data.SQLite.Core
The current source project targets .NET 9 and .NET 10. Building all targets requires the corresponding SDKs or targeting packs.
Use these namespaces for the model and query examples:
using Gurux.Service.Orm;
using Gurux.Service.Orm.Common;
using Gurux.Service.Orm.Common.Enums;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
⚙️ Key Attributes
| Attribute | Description |
|---|---|
DataMember |
Marks a field to be stored in the database. |
AutoIncrement |
Increases on insert; value is written back to the object. |
ForeignKey |
Defines a foreign key relation. |
Relation |
Declares a relation between tables. |
Alias |
SQL alias for fields/tables. |
StringLength(int max) |
Specifies the maximum string length in the schema. |
IsRequired |
Field must have a value (NOT NULL). |
🔗 Relations
Gurux ORM supports:
- 1:1 — One-to-One
- 1:N — One-to-Many
- N:N — Many-to-Many
🏗️ Defining C# Classes
Implement IUnique<T> on entities that expose an Id property, as shown below. Use DataContract and DataMember to declare the mapped model and its columns.
UseEnumStringValue and UseEpochTimeFormat are database settings, rather than model attributes.
Models
Country ↔ Company (1:N)
GXCountry maintains a list of its companies and cascades deletes.
[DataContract]
class GXCountry : IUnique<int>
{
[DataMember(Name = "ID"), AutoIncrement]
public int Id { get; set; }
[DataMember(Name = "CountryName"), IsRequired, StringLength(100)]
public string Name { get; set; }
[DataMember]
[ForeignKey(OnDelete = ForeignKeyDelete.Cascade)]
public List<GXCompany> Companies { get; set; }
}
[DataContract]
class GXCompany : IUnique<long>
{
[AutoIncrement, DataMember]
public long Id { get; set; }
[DataMember, IsRequired, StringLength(150)]
public string Name { get; set; }
// Navigation to Country; FK column is auto-managed (e.g., CountryID)
[DataMember(Name = "CountryID"), ForeignKey]
public GXCountry Country { get; set; }
}
Usage Example:
GXCountry finland = new GXCountry { Name = "Finland" };
GXCompany gurux = new GXCompany { Name = "Gurux Ltd", Country = finland };
GXCompany sample = new GXCompany { Name = "Sample Co", Country = finland };
finland.Companies = new List<GXCompany> { gurux, sample };
// Create Country and all related tables in one go (including Company)
Connection.CreateTable<GXCountry>(true, false);
Connection.Insert(GXInsertArgs.Insert(finland));
Company ↔ Users (1:N)
To extend the country/company model, add this property to GXCompany. The GXUser model in the next section holds the corresponding company reference.
[DataMember, ForeignKey(OnDelete = ForeignKeyDelete.Cascade)]
public GXUser[] Users { get; set; }
The following sections extend the same model. Combine the properties in one declaration per class.
Users ↔ UserGroups (N:N)
[DataContract]
class GXUser : IUnique<int>
{
[AutoIncrement, DataMember]
public int Id { get; set; }
[DataMember, IsRequired, StringLength(120)]
public string Name { get; set; }
[DataMember, ForeignKey(typeof(GXUserGroup), typeof(GXUserToUserGroup))]
public GXUserGroup[] Groups { get; set; }
[DataMember, ForeignKey(OnDelete = ForeignKeyDelete.Cascade)]
public GXCompany Company { get; set; }
}
[DataContract]
class GXUserGroup : IUnique<int>
{
[AutoIncrement, DataMember]
public int Id { get; set; }
[DataMember, IsRequired, StringLength(120)]
public string Name { get; set; }
[DataMember, ForeignKey(typeof(GXUser), typeof(GXUserToUserGroup))]
public GXUser[] Users { get; set; }
}
[DataContract]
class GXUserToUserGroup
{
[DataMember, ForeignKey(typeof(GXUser), OnDelete = ForeignKeyDelete.Cascade)]
public int UserId { get; set; }
[DataMember, ForeignKey(typeof(GXUserGroup), OnDelete = ForeignKeyDelete.Cascade)]
public int GroupId { get; set; }
}
Usage Example:
GXUserGroup adminGroup = new GXUserGroup { Name = "Admin" };
GXUserGroup devGroup = new GXUserGroup { Name = "Developers" };
GXCompany gurux = new GXCompany { Name = "Gurux Ltd" };
GXUser user = new GXUser
{
Name = "Charlie",
Company = gurux,
Groups = new[] { adminGroup, devGroup }
};
// Create UserGroup and all related tables (bridge + users + company) in one go
Connection.CreateTable<GXUserGroup>(true, false);
Connection.Insert(GXInsertArgs.Insert(user));
⚡ Quick Start
The following example is self-contained. It uses an in-memory SQLite database and a simple model to demonstrate create, read, update, and delete operations.
using Gurux.Service.Orm;
using Gurux.Service.Orm.Common;
using System.ComponentModel.DataAnnotations;
using System.Data.SQLite;
using System.Runtime.Serialization;
class Program
{
static void Main()
{
using var sqlite = new SQLiteConnection("Data Source=:memory:");
// GXDbConnection opens the underlying connection if necessary.
var connection = new GXDbConnection(sqlite);
connection.CreateTable<Person>(true, false);
var person = new Person { Name = "Alice" };
connection.Insert(GXInsertArgs.Insert(person));
// The generated ID is written back to person.Id.
var query = GXSelectArgs.Select<Person>(p => "*", p => p.Id == person.Id);
var people = connection.Select<Person>(query);
person.Name = "Alice Updated";
connection.Update(GXUpdateArgs.Update(person, p => p.Name));
connection.Delete(GXDeleteArgs.Delete(person));
}
}
[DataContract]
public class Person : IUnique<int>
{
[DataMember, AutoIncrement]
public int Id { get; set; }
[DataMember, IsRequired, StringLength(120)]
public string Name { get; set; } = string.Empty;
}
The remaining examples use the related GXCountry, GXCompany, GXUser, and GXUserGroup models above. Connection denotes an initialized GXDbConnection.
🔍 Query Examples
N:N Join (Users ↔ Groups)
GXSelectArgs arg = GXSelectArgs.Select<GXUser>(s => "*");
arg.Columns.Add<GXUserGroup>();
arg.Joins.AddInnerJoin<GXUser, GXUserToUserGroup>(j => j.Id, j => j.UserId);
arg.Joins.AddInnerJoin<GXUserToUserGroup, GXUserGroup>(j => j.GroupId, j => j.Id);
var list = Connection.Select<GXUser>(arg);
Filtering & sorting for N:N
Only users in the "Developers" group, ascending by user name:
GXSelectArgs arg = GXSelectArgs.Select<GXUser>(s => "*");
arg.Columns.Add<GXUserGroup>();
arg.Joins.AddInnerJoin<GXUser, GXUserToUserGroup>(j => j.Id, j => j.UserId);
arg.Joins.AddInnerJoin<GXUserToUserGroup, GXUserGroup>(j => j.GroupId, j => j.Id);
// Filter: group name equals "Developers"
arg.Where.And<GXUserGroup>(g => g.Name == "Developers");
// Sort: users by Name (ascending)
arg.OrderBy.Add<GXUser>(u => u.Name);
var developers = Connection.Select<GXUser>(arg);
Alternative (portable) approach using a subquery:
// Subquery: find group IDs with name 'Developers'
var groupsSub = GXSelectArgs.Select<GXUserGroup>(g => g.Id, g => g.Name == "Developers");
// Users whose Groups contain one of the subquery results
var usersInDevs = GXSelectArgs.Select<GXUser>(
s => "*",
u => GXSql.In(u.Groups, groupsSub)
);
// Optional sort
usersInDevs.OrderBy.Add<GXUser>(u => u.Name);
var developers2 = Connection.Select<GXUser>(usersInDevs);
Separate blocks for filtering and sorting
Filtering only
GXSelectArgs arg = GXSelectArgs.Select<GXUser>(s => "*");
arg.Where.And<GXUser>(c => c.Name.StartsWith("Cha"));
var filtered = Connection.Select<GXUser>(arg);
Sorting only
GXSelectArgs arg = GXSelectArgs.Select<GXUser>(s => "*");
arg.OrderBy.Add<GXUser>(c => c.Name);
var sorted = Connection.Select<GXUser>(arg);
You can also combine:
GXSelectArgs arg = GXSelectArgs.Select<GXUser>(s => "*"); arg.Where.And<GXUser>(c => c.Name.StartsWith("Cha")); arg.OrderBy.Add<GXUser>(c => c.Name); var result = Connection.Select<GXUser>(arg);
Selecting Related Data (1:N Join Example)
GXSelectArgs arg = GXSelectArgs.SelectAll<GXCountry>();
arg.Columns.Add<GXCompany>();
arg.Joins.AddInnerJoin<GXCountry, GXCompany>(c => c.Id, c => c.Country);
List<GXCountry> countries = Connection.Select<GXCountry>(arg);
🚫 Exclude<T>() (Select & Update)
Use Exclude<T>() to omit specific fields from queries and updates. Great for skipping heavy columns on read and avoiding server-managed columns on update.
Exclude in Select
GXSelectArgs args = GXSelectArgs.SelectAll<GXUser>();
args.Exclude<GXUser>(q => q.Company);
var users = Connection.Select<GXUser>(args);
Exclude in Update
// user is an existing GXUser instance.
GXUpdateArgs args = GXUpdateArgs.Update(user);
args.Exclude<GXUser>(q => new { q.Id, q.Company });
Connection.Update(args);
For a targeted update, explicitly select the properties to change, as shown below.
🔁 Update
user.Name = "UpdatedName";
Connection.Update(GXUpdateArgs.Update(user, q => q.Name));
🛠️ Schema Management
GXSchemaManager is used when you need to create, inspect, update, rename, or drop database schema objects directly from ORM model classes.
using Gurux.Service.Orm;
using Gurux.Service.Orm.Model;
GXDbConnection Connection = new GXDbConnection(dbConnection, null);
GXSchemaManager manager = new GXSchemaManager(Connection);
// Create the table from the current model.
manager.CreateTable<GXUser>(createRelations: true, ignoreErrors: false);
// Update an existing table to match the current model.
manager.UpdateTable<GXUser>();
// Inspect the current database schema.
GXTableSchema schema = manager.Describe<GXUser>();
// Rename a table or column when needed.
manager.RenameTable<GXUser>("Users");
manager.RenameTableColumn<GXUser>(q => q.Name, "UserName");
Changing a Column Data Type
To change a column from one data type to another, update the C# model property type and call UpdateTable<T>(). GXSchemaManager compares the existing database table with the current model, creates a temporary converted column, copies converted values into it, drops the old column, and renames the converted column back to the original name.
For example, if GXDevice.SerialNumber was previously stored as string and you want to store it as int, change the model:
[DataContract]
class GXDevice : IUnique<int>
{
[DataMember, AutoIncrement]
public int Id { get; set; }
[DataMember]
public int SerialNumber { get; set; }
}
Then update the table:
GXSchemaManager manager = new GXSchemaManager(Connection);
manager.UpdateTable<GXDevice>();
The default conversion uses the database settings' normal type conversion. This is enough for simple conversions such as numeric text to numeric values when all existing values are valid.
Custom Value Conversion
Use ColumnValueConverting when existing values need special handling during a column type change. The event is raised for each distinct non-null value before it is written into the converted column.
Set e.Value to the converted value and e.IsConverted to true when your handler has completed the conversion. If IsConverted is left as false, Gurux ORM uses the default conversion.
EventHandler<GXColumnValueConvertingEventArgs> converter = (sender, e) =>
{
if (e.TableName == "GXDevice" &&
e.ColumnName == "SerialNumber" &&
e.OldType == typeof(string) &&
e.NewType == typeof(int))
{
string value = (string)e.Value;
// Example: convert values like "SN-12345" to 12345.
e.Value = int.Parse(value.Replace("SN-", ""));
e.IsConverted = true;
}
};
try
{
manager.ColumnValueConverting += converter;
manager.UpdateTable<GXDevice>();
}
finally
{
manager.ColumnValueConverting -= converter;
}
GXColumnValueConvertingEventArgs contains:
| Property | Description |
|---|---|
TableName |
Name of the table being updated. |
ColumnName |
Name of the column being converted. |
OldType |
Previous C# type mapped from the existing database column. |
NewType |
New C# type from the current model. |
Value |
Current value. Set this to the converted value. |
IsConverted |
Set to true when the handler has converted Value. |
❌ Delete
// Preferred when you have the instance:
Connection.Delete(GXDeleteArgs.Delete(user));
// If only the ID is known:
Connection.Delete(GXDeleteArgs.DeleteById<GXUser>(5));
🧠 Subqueries
GXSelectArgs sub = GXSelectArgs.Select<GXUser>(q => q.Id, q => q.Id > 100);
GXSelectArgs arg = GXSelectArgs.Select<GXUserGroup>(null, q => GXSql.In(q.Users, sub));
Checking Whether a Table Is Empty
GXSelectArgs arg = GXSelectArgs.IsEmpty<GXUser>();
if (Connection.SingleOrDefault<bool>(arg))
{
// Table is empty
}
else
{
// Table has at least 1 record
}
📚 Further Resources
For additional examples and database-specific tests, see Gurux.Service_Simple_UnitTests.
To build from the repository root:
dotnet build Development/Gurux.Service.sln
Need help?
Ask your questions on the Gurux Forum.
License
Gurux ORM is available under either of the following licenses:
- Open-source license: GNU General Public License, version 2 only (GPL-2.0-only). See LICENSE for the full license text.
- Commercial license: Available from Gurux Ltd under a separate written agreement. This option allows use under the terms of that agreement instead of GPL-2.0-only.
A valid commercial license is required to use the commercial licensing option. See Gurux Commercial License Notice.
For commercial licensing inquiries, contact Gurux Ltd.
| Product | Versions 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. |
-
net10.0
- Gurux.Service.Common (>= 3.0.2611.501)
- System.Data.Odbc (>= 10.0.14)
- System.Data.SqlClient (>= 4.9.1)
-
net9.0
- Gurux.Service.Common (>= 3.0.2611.501)
- System.Data.Odbc (>= 10.0.14)
- System.Data.SqlClient (>= 4.9.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Gurux.Service:
| Package | Downloads |
|---|---|
|
Gurux.DLMS.AMI.Messages
Gurux.DLMS.AMI.Messages implements interfaces that are needed for Gurux DLMS AMI. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.2611.501 | 217 | 11/5/2026 |
| 3.0.2611.401 | 99 | 11/4/2026 |
| 3.0.2611.301 | 110 | 11/3/2026 |
| 3.0.2611.201 | 104 | 11/2/2026 |
| 3.0.2610.2802 | 122 | 10/28/2026 |
| 3.0.2610.2801 | 107 | 10/28/2026 |
| 3.0.2610.2702 | 108 | 10/27/2026 |
| 3.0.2610.2701 | 107 | 10/27/2026 |
| 3.0.2610.2604 | 102 | 10/26/2026 |
| 3.0.2610.2603 | 98 | 10/26/2026 |
| 3.0.2610.2602 | 99 | 10/26/2026 |
| 3.0.2610.2601 | 101 | 10/26/2026 |
| 3.0.2610.2102 | 105 | 10/21/2026 |
| 3.0.2610.2101 | 105 | 10/21/2026 |
| 3.0.2610.902 | 112 | 10/9/2026 |
| 3.0.2610.601 | 104 | 10/6/2026 |
| 3.0.2610.501 | 114 | 10/5/2026 |
| 3.0.2610.208 | 125 | 10/2/2026 |
| 3.0.2610.207 | 101 | 10/2/2026 |
| 3.0.2610.206 | 101 | 10/2/2026 |