使用Fluent Nhibernate使用基于实体的键映射字典

时间:2012-03-12 13:09:42

标签: nhibernate fluent-nhibernate

我有以下NHibernate HBM按预期工作,但它使我的代码变脆,因为类可能会改变,我想在FNH中进行映射,但我尽量尝试,我似乎无法让它工作,特别是如何获得多对多索引:

<map name="Permissions" table="PermissionsBySet" cascade="all">
  <key column="PermissionSet_id" />
  <index-many-to-many class="Picomole.ReadModel.Permission, Picomole.ReadModel" column="PermissionId" />
  <element column="PermissionType" type="Picomole.ReadModel.PermissionType, Picomole.ReadModel" not-null="true" />
</map>

鉴于以下类别:

public class PermissionSet : DomainObject
{
    public virtual PermissionSet Defaults { get; set; }

    public virtual IDictionary<Permission, PermissionType> Permissions { get; set; }
}

public class Permission : DomainObject
{
    public virtual string Controller { get; set; }

    public virtual string Action { get; set; }
}

public enum PermissionType
{
    None,
    Read,
    Write,
    Grant
}

1 个答案:

答案 0 :(得分:1)

感谢Firo指出我正确的方向。我试图做的最终答案是:

HasManyToMany(x => x.Permissions)
                .AsEntityMap("PermissionId", "PermissionLevel")
                .Element("PermissionLevel", x => x.Type<PermissionLevel>())
                .Table("PermissionsBySet");

由于我遇到一些关于会话连接被关闭的奇怪错误的问题,我不得不补充:

                .Fetch.Join()
                .Not.LazyLoad()