【问题标题】:Fluent Nhibernate HasManyToMany InheritanceFluent Nhibernate HasManyToMany 继承
【发布时间】:2013-12-27 20:54:52
【问题描述】:
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");
        });
    }
}

绘图和互联网的映射相同

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

现在,如果插入数据可以正常工作,则数据已正确存储在相应的表中 但是,当尝试获取数据时出现问题

假设插入 2 个文学资源和 1 个互联网资源。 当从表中获取数据时,我有 3 条文献记录(2 条记录填充 1 条空或空)和 3 条互联网记录(1 条填充和 2 条空)。请帮我纠正映射

【问题讨论】:

    标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    看起来,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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多