NMemory enables to define relations between tables that will prevent foreign key violation. This requires to have indexes built on the foreign key field and on the unique key field. You have to specify these indexes and the data conversions between the indexed fields. The following example demonstrates a creation of a one to many relation between people and groups.

public class MyDatabase : Database
{
    public MyDatabase()
    {
        var peopleTable = base.Tables.Create<Person, int>(p => p.Id);
        var groupTable = base.Tables.Create<Group, int>(g => g.Id);

        var peopleGroupIdIndex = peopleTable.CreateIndex(
            new RedBlackTreeIndexFactory<Person>(), 
            p => p.GroupId);

        this.Tables.CreateRelation(
            groupTable.PrimaryKeyIndex, 
            peopleGroupIdIndex, 
            x => x, 
            x => x);

        this.People = peopleTable;
        this.Groups = groupTable;
    }

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

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

Last edited Jun 11, 2012 at 9:56 AM by tamasflamich, version 6

Comments

No comments yet.