Mapping Dictionary with entity based key using Fluent Nhibernate

↘锁芯ラ 提交于 2019-12-12 02:33:56

问题


I have the following NHibernate HBM that works as expected, but it makes my code brittle as the classes may change and I want to do the mapping in FNH, but try as I might, I cannot seem to get it to work, especially how you get the index-many-to-many:

<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>

Given the following classes:

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:


public class PermissionSetMap : ClassMap<PermissionSet>
{
    public PermissionSetMap()
    {
        HasManyToMany(ps => ps.Permissions)
            .AsEntityMap("permissions_id")
            .Element("PermissionType");
    }
}



回答2:


Thanks Firo for point me in the right direction. The final answer for what I was attempting to do was:

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

And because of some issues I was having with a strange error about the session connection being closed, I had to add:

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


来源:https://stackoverflow.com/questions/9667620/mapping-dictionary-with-entity-based-key-using-fluent-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!