2012-01-24 96 views
1

我創建了2個對象:NHibernate.Mapping.ByCode許多一對多關係

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

和2映射:

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

莫名其妙的關係不因爲當我嘗試使用上面的代碼添加到各種例如設置和提交那時我還沒有工作查看保存到SetsToSorts鏈接表的任何記錄。

有沒有人有線索我在映射中失蹤?否則做錯了?

謝謝 Joost的

回答

1

你映射說,設置的分類收集是逆(map.Inverse(真))。這意味着雙向關聯的另一方負責持續改變。 但是你的排序類映射沒有任何集合映射。在SetMapping上刪除map.Inverse(true)或將非反向集合映射添加到SortMapping。

+0

嗨Hival,非常感謝! 一旦你明白某事正在做什麼,生活就會變得如此簡單。我很親密:) –