NMemory provides a convenient way to define the schema of your in-memory database. The relational data tables are generic collections that contain strongly typed plain old CLR objects that represent the table entities. The following example shows a typical table element declaration.

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

    public string Name { get; set; }

    public int GroupId { get; set; }

    public DateTime BirthDay { get; set; }
}

It is recommended to create a Database subclass class that provides endpoints for the tables and creates the structure of your database in its constructor. Creating a table requires to specify the primary key field(s) of the contained entities. The following example shows how to define a table for the previously presented entity.

public class MyDatabase : Database
{
    public MyDatabase()
    {
        this.People = base.Tables.Create<Person, int>(p => p.Id);
    }

    public ITable<Person> People { get; private set; }
}

Last edited Aug 15, 2012 at 11:14 PM by tamasflamich, version 9

Comments

tamasflamich Apr 29, 2014 at 12:29 PM 
Just pass a null reference. You can use the GeneratedGuidConstaint for autogenerated guids.

var people = base.Tables.Create<Person, Guid>(p => p.Id, null);
people.AddConstraint(new GeneratedGuidConstraint<Person>(x => x.Id));

this.People = people;

hulk2484 Apr 28, 2014 at 9:03 PM 
How do you not make it an int? All Create overloads for the TableCollection require you to pass in an IndentityReference. What if I wanted to make it a GUID?

tamasflamich Dec 14, 2013 at 10:04 AM 
It is not mandatory to make the pk an identity field.

Check the GitHub mirror here:
https://github.com/tamasflamich/nmemory

Juri Dec 13, 2013 at 10:13 AM 
It seems like the docs lack something. Shouldn't this be
this.People = base.Tables.Create<Person, int>(p => p.Id, new IdentitySpecification<Person>(x=>x.Id));

Btw, why don't you host this lib on GitHub, it's way easier to contribute :)