好的我对Fluent和NHibernate很陌生,我不知道如何映射我数据库中存在的这种特定关系。我在下面的ER图中概述了我的表格结构。
下面是我当前的C#实体类和Fluent映射:
public class GroupHeader
{
public virtual Guid Id { get; private set;}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<RightHeader> AllowedRights { get; set; }
public virtual IList<RightHeader> DeniedRights { get; set; }
public virtual IList<RightHeader> UnsetRights { get; set; }
}
public class RightHeader
{
public virtual decimal Num { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class GroupHeaderMap : ClassMap<GroupHeader>
{
public GroupHeaderMap()
{
Table("GROUP_HEADER");
Id(x => x.Id, "GROUP_ID");
Map(x => x.Name, "GROUP_NAME").Unique();
Map(x => x.Description, "GROUP_DESCRIPTION");
HasManyToMany(x => x.AllowedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.DeniedRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = -1")
.Cascade.SaveUpdate();
HasManyToMany(x => x.UnsetRights)
.Table("GROUP_RIGHT_COMPOSITE")
.ParentKeyColumn("GROUP_ID")
.ChildKeyColumn("RIGHT_NUM")
.Where("RIGHT_VALUE = 0")
.Cascade.SaveUpdate();
}
}
public class RightHeaderMap : ClassMap<RightHeader>
{
public RightHeaderMap()
{
Table("RIGHT_HEADER");
Id(x => x.Num, "RIGHT_NUM");
Map(x => x.Name, "RIGHT_NAME");
Map(x => x.Description, "RIGHT_DESCRIPTION");
}
}
目前,每当我创建新的GroupHeader并填充AllowedRights成员变量时,它都无法插入这些记录,因为GROUP_RIGHT_COMPOSITE中的RIGHT_VALUE列不允许空值。我不太确定如何用流利的方式映射它,或者我应该在我的实体中真正处理它。任何帮助将不胜感激。