I have an entity class that represents a person and an enum that represents permissions that a person has. I am trying to map this relationship to a database using nhibernate mapping by code without any success.
The code looks like this:
public enum Permissions
{
None = 1,
CanUpdate = 2,
CanInsert = 3,
CanDelete = 4
}
public class Person
{
private ICollection<Permissions> permissions;
public Person()
{
this.permissions = new Collection<Permissions>();
}
public virtual ICollection<Permissions> Permissions
{
get
{
return this.permissions;
}
}
}
public class PersonMap : ClassMapping<Person>
{
public PersonMap()
{
this.Set(
x => x.Permissions,
m =>
{
m.Access(Accessor.Field);
m.Key(k => k.Column("PersonId"));
m.Table("PersonHasPermission");
},
map => map.Element(
p =>
{
p.Column("PermissionId");
p.Type<NHibernate.Type.EnumType<Permissions>>();
}));
}
}
The database tables look like this:
Person
-----------------------
PersonId (PK, uniqueidentifier, not null)
Name (nvarchar(max), not null)
PersonHasPermission
-----------------------
PersonId (PK, FK, uniqueidentifier, not null)
PermissionId (PK, FK, int, not null)
So, with this configuration I do not get any exceptions but whenever I try to fetch the permissions for a person the collection is always empty even though there is data in the database.
I'm hoping that the code above is explains what I am trying to achieve, but do let know if further clarity is required.
Any help with this would be much appreciated. Thanks in advance.