1
votes

I'm trying to figure out the best way to accomplish a design that's persisted with NHibernate. Here's what the data would ideally look like:

Person
===============
Id (int)
Name (nvarchar)


Privileges
==============
Person_Id
Right (int)
AccessLevel (int)

Ideally, I'd be able to create classes like this (pretend I have all the NHibernate virtual modifiers):

public enum Rights
{
    Read = 0,
    Save = 1,
    Delete = 2
}

public enum AccessLevels
{
    LevelOne = 0,
    LevelTwo = 1,
    LevelThree = 2
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Dictionary<Rights, AccessLevels> Privileges { get; set; }
}

Originally I considered the design described in this blog post, but I don't want to make the Rights enum a class because I'd like to be able to test for it without having to keep track of the Id of a given right. A pair of enumerations would be easiest I think.

Anyway, I've been striking out figuring out how to accomplish this in NHibernate, much less Fluent NHibernate. I'm hoping some NH gurus might be able to help.

1

1 Answers

0
votes

I'm not sure if it's possible... but try something like this (I did not test):

 HasMany(x => x.Privileges)
            .WithTableName("Privileges")
            .KeyColumnNames.Add("Person_Id")
            .Cascade.All()
            .AsMap<string>(
                index => index.WithColumn("Right").WithType<Rights>(),
                element => element.WithColumn("AccessLevel").WithType<AccessLevels>()
            );
    }

Or something like this:

References(x => x.Privileges).AsMap<Rights>("Right").Element("AccessLevel", c => c.Type<AccessLevels>());.

Take a look at this thread: How do I map a dictionary using Fluent NHibernate automapping?