Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate: mapping a dictionary of lists

I have a DB with the following tables:

  • Roles: [Id, Name, ...]
  • Permissions: [Id, Name, ...]
  • MetaDataDefinitions: [Id, Name, ...]
  • RolePermissions: [Id, RoleId, PermissionId, MetaDataDefinitionId]

POCO classes are:

class Role {
public Role() {
    MetaDataPermissions = new Dictionary<MetaDataDefinition, Permission>() ;
}

public virtual int Id { get; set; }
public virtual string Name { get; set; }

public virtual IDictionary<MetaDataDefinition, Permission> MetaDataPermissions { get; set; }
}


class Permission {
public Permission() {
    MetaDataRoles = new Dictionary<MetaDataDefinition, Role>() ;
}

public virtual int Id { get; set; }
public virtual string Name { get; set; }

public virtual IDictionary<Role, MetaData> RolesMetaData { get; set; }
}

class MetaData {
public MetaData() {
    RolePermissions = new Dictionary<Role, Permission>() ;
}

public virtual int Id { get; set; }
public virtual string Name { get; set; }

public virtual IDictionary<Role, Permission> RolePermissions { get; set; }
}

Fluent mappings:

class RoleMap : ClassMap<Role> {
public RoleMap() {
    [...]
    HasManyToMany( x => x.MetaDataPermissions )
        .Table( "RolePermissions" )
        .AsEntityMap( "MetaDataDefinitionId", "PermissionId" )
        .KeyColumn( "RoleId" ) ;
    [...]
}
}

class PermissionMap : ClassMap<Permission> {
public PermissionMap() {
    [...]
    HasManyToMany( x => x.RolesMetaData )
        .Table( "RolePermissions" )
        .AsEntityMap( "RoleId", "MetaDataDefinitionId" )
        .KeyColumn( "PermissionId" ) ;
    [...]
}
}

class MetaDataMap : ClassMap<MetaData> {
public MetaDataMap() {
    [...]
    HasManyToMany( x => x.RolePermissions )
        .Table( "RolePermissions" )
        .AsEntityMap( "RoleId", "PermissionId" )
        .KeyColumn( "MetaDataDefinitionId" ) ;
    [...]
}
}

But in POCO classes i need this:

public virtual IDictionary<Role, IList<MetaData>> RolesMetaData { get; set; }

How such dictionaries should be represented in the mapping?

like image 273
Eugen Popovici Avatar asked Nov 05 '22 03:11

Eugen Popovici


1 Answers

Directly mapping a dictionary of lists is not supported.

like image 155
Diego Mijelshon Avatar answered Nov 09 '22 09:11

Diego Mijelshon