Ok I'm pretty new to Fluent and NHibernate and I'm not sure how to map this specific relationship that exists in my database. I have the following ER diagram below that outlines my table structure.
Below are my current C# entity classes and Fluent mappings:
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");
}
}
Currently whenever I create a new GroupHeader and populate the AllowedRights member variable it fails to insert these records because the RIGHT_VALUE column in GROUP_RIGHT_COMPOSITE doesn't allow nulls. I'm not really sure how to map this in fluent or where I should really handle this in my entities. Any help would be appreciated.