NHibernate.Mapping.ByCode多对多关系

NHibernate.Mapping.ByCode多对多关系,nhibernate,Nhibernate,我创建了两个对象: public class Set { public Set() { _sorts = new List<Sort>(); } public virtual int Id { get; set; } public virtual string Code { get; set; } private ICollection<Sort> _sorts; public virtual ICo

我创建了两个对象:

public class Set
{
    public Set()
    {
        _sorts = new List<Sort>();
    }
    public virtual int Id { get; set; }
    public virtual string Code { get; set; }
    private ICollection<Sort> _sorts;
    public virtual ICollection<Sort> Sorts
    {
        get { return _sorts; }
        set { _sorts = value; }
    }
}

public class Sort
{
    public Sort()
    {
        _sets = new List<Set>();
    }
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    private ICollection<Set> _sets;
    public virtual ICollection<Set> Sets
    {
        get { return _sets; }
        set { _sets = value; }
    }
}
不知何故,这些关系还不起作用,因为当我尝试使用上面的代码添加排序到set-example和commit时,我没有看到任何保存到SetsToSorts链接表的记录

有人知道我在地图上遗漏了什么吗?还是做错事

谢谢,,
Joost

您的映射表示集合的排序集合是反向的(map.inverse(true))。这意味着双向关联的另一端负责持久化更改。
但是排序类映射没有任何集合映射。删除SetMapping上的map.Inverse(true)或将非反向集合映射添加到SortMapping。

您的映射表示集合的排序集合是反向的(map.Inverse(true))。这意味着双向关联的另一端负责持久化更改。
但是排序类映射没有任何集合映射。删除映射。在SetMapping上反转(true)或将非反转集合映射添加到SortMapping。

Hi Hival,非常感谢!一旦你明白了什么在做,生活就是如此简单。我是如此接近:)嗨,海瓦尔,非常感谢你!一旦你明白了什么在做,生活就是如此简单。我是如此接近:)
public class SetMapping: ClassMapping<Set>
    {
        public SetMapping()
        {
            Table("Sets");
            Id(x => x.Id, map => map.Generator(IdGeneratorSelector.CreateGenerator()));
            Property(x => x.Code, map =>
            {
                map.Length(50);
                map.NotNullable(false);
            });
            Bag(x => x.Sorts, map =>
            {
                map.Key(k =>
                {
                    k.Column("SetId");
                    k.NotNullable(true);
                });
                map.Cascade(Cascade.All);
                map.Table("SetsToSorts");
                map.Inverse(true);

            }, r => r.ManyToMany(m => m.Column("SortId")));
        }
    }

    public class SortMapping: ClassMapping<Sort>
    {
        public SortMapping()
        {
            Table("Sorts");
            Id(x => x.Id, map => map.Generator(IdGeneratorSelector.CreateGenerator()));
            Property(x => x.Name, map =>
            {
                map.Length(50);
                map.NotNullable(false);
            });
        }
    }
var set = new Set() {Code = "001"};
            var sort = new Sort {Name = "My name"};

            set.Sorts.Add(sort);
            sort.Sets.Add(set);