Nmemory - Getting Started - Create Table - Example 2

CSharp · Console

/Program.cs

// NMemory
// Doc: https://nmemory.net/create-table

// @nuget: NMemory

using System;
using System.Linq;
using NMemory;
using NMemory.Indexes;
using NMemory.Tables;
					
public class Program
{
	public static void Main()
	{
		MyDatabase myDatabase = new MyDatabase();

		myDatabase.Groups.Insert(new Group());

		myDatabase.Groups.Insert(new Group());
		
		FiddleHelper.WriteTable("All Groups",myDatabase.Groups.ToList());
	}
	
	public class MyDatabase : Database
	{
		public MyDatabase()
		{
			var members = this.Tables.Create<Member, int>(x => x.Id, new IdentitySpecification<Member>(x => x.Id, 1, 1));
			var groups = base.Tables.Create<Group, int>(g => g.Id, new IdentitySpecification<Group>(x => x.Id, 1, 1));

			this.Members = members;
			this.Groups = groups;

			RelationOptions options = new RelationOptions(
				cascadedDeletion: true);

			var peopleGroupIdIndex = members.CreateIndex(
				new RedBlackTreeIndexFactory(),
				p => p.GroupId);

			this.Tables.CreateRelation(
				groups.PrimaryKeyIndex,
				peopleGroupIdIndex,
				x =>  x ?? -1,
				x => x,
				options);
		}

		public ITable<Member> Members { get; private set; }

		public ITable<Group> Groups { get; private set; }
	}

	public class Member
	{
		public int Id { get; set; }

		public string Name { get; set; }

		public int? GroupId { get; set; } 
	}

	public class Group
	{
		public int Id { get; set; } 

		public string Name { get; set; }

	}
	
}
;