简体   繁体   中英

Fluent NHibernate many-to-many relationship on existing database

I am mapping an existing database using Fluent NHibernate and have encountered a problem when trying to populate many-to-many collections. The database itself is not set up correctly using foreign keys, here is a simplified example of the problem I'm having.

Tables:

  1. Users
  2. Groups
  3. UsersInGroups

Entity Classes:

public class User
{
    public virtual long UserID { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Group> Groups { get; set; }
}

public class Group
{
    public virtual long GroupID { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<User> Users { get; set; }
}

public class UserInGroup
{
    public virtual User User { get; set; }
    public virtual Group Group { get; set; }

    #region Fluent NHibernate Composite Key Overrides

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        var compare = obj as UserInGroup;

        if (compare == null)
            return false;

        return User.UserID == compare.User.UserID &&
               Group.GroupID == compare.Group.GroupID;
    }

    public override int GetHashCode()
    {
        return (User.UserID + "|" + Group.GroupID).GetHashCode();
    }

    #endregion
}

Mapping Classes:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("Users");
        Id(x => x.UserID, "UserID").GeneratedBy.Identity();

        Map(x => x.Name, "Name");

        HasManyToMany(x => x.Groups).Table("UsersInGroups")
                                    .ParentKeyColumn("GroupID")
                                    .ChildKeyColumn("UserID")
                                    .Cascade.All();
    }
}

public class GroupMap : ClassMap<Group>
{
    public GroupMap()
    {
        Table("Groups");
        Id(x => x.GroupID, "GroupID").GeneratedBy.Identity();

        Map(x => x.Name, "Name");

        HasManyToMany(x => x.Users).Table("UsersInGroups")
                                   .ParentKeyColumn("UserID")
                                   .ChildKeyColumn("GroupID")
                                   .Inverse();
    }
}

public class UserInGroupMap : ClassMap<UserInGroup>
{
    public UserInGroupMap()
    {
        Table("UsersInGroups");

        CompositeId().KeyReference(x => x.User, "UserID")
                     .KeyReference(x => x.Group, "GroupID");
    }
}

In my configuration I have eager loading enabled, the HasMany relationships seem to be working okay but the HasManyToMany relationships are returning an empty collection. I have tried a few different combinations of Cascade.All() and Inverse on the collections all with no success. Any help would be much appreciated, thanks.

HasManyToMany(x => x.Users).Table("UsersInGroups")
                           .ParentKeyColumn("UserID")
                           .ChildKeyColumn("GroupID")
                           .Not.LazyLoad();
    HasManyToMany(x => x.Users).Table("UsersInGroups") 
      .ParentKeyColumn("GroupID")
      .ChildKeyColumn("UserID")
      .Not.LazyLoad();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM