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個空)。請幫我正確的映射