2013-12-23 159 views
0
public class Student 
{ 
    public int StudentId { get; set; } 
    public string StudentName { get; set; } 
    public List<Literature> LiteratureCources { get; set; } 
    public List<Drawing> DrawingCources { get; set; } 
    public List<Internet> InternetCources { get; set; } 
} 

public class cource 
{ 
    public int CourceId { get; set; } 
    public string CourceName { get; set; } 
} 

public class Literature : cource 
{ 
    public LiteratureType LiteratureType { get; set; } 
} 

public class Drawing : cource 
{ 
    public DrawingType DrawingType { get; set; } 
} 

public class Internet : cource 
{ 
    public InternetType InternetType { get; set; } 
} 
public enum LiteratureType 
{ 
    L1, L2, L3 
} 

public enum DrawingType 
{ 
    D1, D2, D3 
} 
public enum InternetType 
{ 
    I1, I2, I3 
} 

Tables 
------ 

Student 
------- 
StudentId 
StudentName 

Cource 
------ 
CourceId (PK) 
CourceName 

InternetCource 
-------------- 
InternetCourceId (FK) 
InternetType 

DrawingCource 
------------- 
DrawingCourceId (FK) 
DrawingType 

LiteratureCource 
---------------- 
LiteratureCourceId (FK) 
LiteratureType 

StudentCource (mybridge table) 
------------- 
StudentId 
CourceId 

public class LiteratureCourceMap:ClassMap<Literature> 
{ 
    public LiteratureCourceMap() 
    { 
     Table("Cource"); 
     ID(a=>a.CourceId,"CourceId"); 
     Map(a=>a.CourceName,"CourceName"); 

     Join("LiteratureCource",a=>{ 
      a.KeyColumn("CourceId"); 
     }); 
    } 
} 

用於繪製相同的映射和互聯網功能NHibernate HasManyToMany繼承

public class StudentMap:ClassMap<Student> 
{ 
    public StudentMap() 
    { 
     Table("Student"); 
     ID(a=>a.StudentId,"StudentId"); 
     Map(a=>a.StudentName,"StudentName"); 

     HasManyToMany(a=>a.InternetCources).Table("StudentCource"). 
      ParentKeyColumn("StudentId"). 
      ChildKeColumn("CourceId"). 
      Cascade.All(); 

     HasManyToMany(a=>a.DrawingCources).Table("StudentCource"). 
      ParentKeyColumn("StudentId"). 
      ChildKeColumn("CourceId"). 
      Cascade.All(); 

     HasManyToMany(a=>a.LiteratureCources).Table("StudentCource"). 
      ParentKeyColumn("StudentId"). 
      ChildKeColumn("CourceId"). 
      Cascade.All(); 
    } 
} 

現在時,如果插入數據,它工作正常數據被正確地存儲在相應的表然而,如果嘗試取數據的話mething出錯

讓我們說如果插入2文獻資源和1互聯網cource。 如果從表中獲取數據,我有3個文獻記錄(2個記錄填充1個空或空)和3個互聯網記錄(1個填充和2個空)。請幫我正確的映射

回答

0

看來,NHibernate總是從Cource表中取,因爲有主鍵。你應該嘗試子類映射。看到這個例子:

public class CourceMap : ClassMap<Cource> 
{ 
    public CourceMap() 
    { 
    Id(x => x.CouerceId); 
    Map(x => x.CourceName); 
    } 
} 

public class DrawingMap : SubclassMap<Cource> 
{ 
    public DrawingMap() 
    { 
    Map(x => x.DrawingType); 
    } 
} 

請參閱本進一步閱讀:https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping