2013-12-23 110 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