.NET Embedded
Run Traverse as an embedded graph database inside your .NET application, with no separate server required.
dotnet add package Traverse.Embedded
Targets net10.0. The Traverse.Embedded package ships with prebuilt native binaries for x64 Windows and x64 Linux, so the database runs directly inside your application with no network overhead.
Open a Database
using Traverse.NET;
// Open or create a database file
using var db = Database.Open("mydata.tvdb");
// With license key
using var db = Database.OpenLicensed("mydata.tvdb", "tskey_your_key_here");
The database file is memory-mapped. Call Flush() to write dirty pages to disk, or let Dispose() handle it.
File Size Limit
// Default: 80% of system RAM
Database.MaxFileSizeBytes = 4L * 1024 * 1024 * 1024; // 4 GB
Transactions
Write operations require a transaction. Traverse uses MVCC for concurrent readers.
using var tx = db.BeginTransaction();
var result = tx.Query("CREATE (n:Person {name: 'Alice', age: 30}) RETURN n");
foreach (var row in result)
{
Console.WriteLine(row.GetString("n.name"));
}
tx.Commit();
// or let Dispose() auto-abort if not committed
Query Results
QueryResult is a forward-only cursor. Access columns and rows:
using var result = tx.Query("MATCH (n:Person) RETURN n.name, n.age");
// Column metadata
foreach (var col in result.Columns)
Console.WriteLine($"{col.Index}: {col.Name}");
// Iterate rows
foreach (var row in result)
{
if (!row.IsNull(0))
{
string name = row.GetString("n.name");
long age = row.GetInt64("n.age");
}
}
// Or materialize all rows
var rows = result.ToList();
Row Access Methods
| Method | Return Type | Description |
|---|---|---|
GetString(int col) | string? | String value by column index |
GetString(string name) | string? | String value by column name |
GetInt64(int col) | long | Integer value by column index |
GetInt64(string name) | long | Integer value by column name |
GetDouble(int col) | double | Float value by column index |
GetDouble(string name) | double | Float value by column name |
GetBool(int col) | bool | Boolean value by column index |
GetBool(string name) | bool | Boolean value by column name |
GetValue(int col) | object? | Boxed typed value |
IsNull(int col) | bool | Check if value is null |
GetTypeName(int col) | string | Type name (INTEGER, FLOAT, STRING, NODE, EDGE, PATH, etc.) |
Snapshot() | Row | Create a managed copy valid beyond cursor lifetime |
Bulk Loading
For high-throughput imports, use BulkWriter. It bypasses MVCC for maximum write speed.
using var bulk = db.BeginBulkWrite();
// Create nodes (returns node ID)
long alice = bulk.CreateNode(
new[] { "Person" },
new[] { "name", "age" },
new[] { "Alice", "30" }); // values are auto-type-inferred
long bob = bulk.CreateNode(
new[] { "Person" },
new[] { "name", "age" },
new[] { "Bob", "25" });
// Create edges
bulk.CreateEdge(alice, bob, "KNOWS",
new[] { "since" },
new[] { "2024" });
// Create indexes
bulk.CreateIndex("idx_person_name", "Person", "name");
bulk.CreateEdgeIndex("idx_knows", "KNOWS");
bulk.Commit();
BulkWriter Methods
| Method | Description |
|---|---|
CreateNode(labels, propKeys, propValues) | Create a node, returns node ID |
CreateEdge(source, target, type, propKeys, propValues) | Create a relationship, returns edge ID |
CreateIndex(name, label, property) | Create a node property index |
CreateEdgeIndex(name, edgeType, property) | Create an edge property index |
CreateEdgeIndex(name, edgeType) | Create a type-only edge index |
DropEdgeIndex(name) | Drop an edge index |
Commit() | Finalize and flush to disk |
Abort() | Discard without flushing |
Error Handling
try
{
tx.Query("INVALID CYPHER");
}
catch (TraverseException ex)
{
Console.WriteLine(ex.ErrorCode); // TvErrorCode.Syntax
Console.WriteLine(ex.Message);
}
Error Codes
| Code | Description |
|---|---|
Io | File I/O error |
Corrupt | Database file corruption detected |
Locked | Database file is locked by another process |
Syntax | Cypher syntax error |
Type | Type mismatch |
Semantic | Semantic query error |
Graph | Graph constraint violation |
Transaction | Transaction conflict or abort |
InvalidArg | Invalid argument |
Internal | Internal engine error |
ResourceExhausted | Memory or file size limit exceeded |