0
votes

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.

ER Diagram

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.

1

1 Answers

0
votes

The problem I found with the above mapping is that I was trying to treat the USER_GROUP_COMPOSITE table as a relationship and not as an entity itself. I have modified the above code such that a new entity called GroupRight exists and has references to both GroupHeader and RightHeader inside the entity.

I think this solved my overall question of how I should map these but I am still having problems persisting this data to the database. My modified question (including updates to my entities and mappings) can be found here Post

The articles that I found useful in solving this issue were: Link 1 Link 2