流畅的NHibernate在现有数据库上的多对多关系

时间:2013-09-12 10:49:39

标签: c# nhibernate fluent-nhibernate fluent fluent-nhibernate-mapping

我正在使用Fluent NHibernate映射现有数据库,并且在尝试填充多对多集合时遇到了问题。使用外键没有正确设置数据库本身,这是我遇到的问题的简化示例。

表:

  1. 用户
  2. UsersInGroups

  3. 实体类:

    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
    }
    

    映射类:

    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");
        }
    }
    

    在我的配置中,我启用了热切加载,HasMany关系似乎工作正常,但HasManyToMany关系返回一个空集合。我在集合上尝试了几种不同的Cascade.All()和Inverse组合,但都没有成功。非常感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF 
fig = go.Figure()
data_matrix = [['Tracer gas/nm', '200', '300','400','500'],['HCHO', "", ],
               ['CHOCHO',],['BrO', ], ['O3', ],['O2', ],['NO2',]]

table = FF.create_table(data_matrix)
py.iplot(table, filename='simple_table')
# py.image.save_as(fig, filename='a-simple-plot.png')