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