我在我的C#应用程序中使用FluentNhibernate我想知道如何连接三个没有定义外键的表。让我们假设我有以下表结构,
学生[StudentID,Name1,Name2,ClassID]
班级[ClassID,姓名,学校ID]
学校[SchoolID,SchoolName]
我需要加入以上三个表格
SELECT a.Name1,a.Name2,b.Name,c.SchoolName FROM Student a, Class b, School c WHERE a.ClassID = b.ClassID AND b.SchoolID = c.SchoolID
我有以下ORM的类结构
public class Student
{
public virtual int StudentID { get; set; }
public virtual string Name1 { get; set; }
public virtual string Name2 { get; set; }
public virtual int ClassID { get; set; }
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentID ).Column("student_id");
Map(x => x.Name1 ).Column("name_1");
Map(x => x.Name1 ).Column("name_2");
Map(x => x.ClassID).Column("class_Id");
Table("student");
}
}
public class Classt
{
public virtual int ClassID { get; set; }
public virtual string Name { get; set; }
public virtual int SchoolID { get; set; }
}
public class ClassMap : ClassMap<Class>
{
public ClassMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
Map(x => x.SchoolID).Column("school_Id");
Table("class");
}
}
public class School
{
public virtual int SchooID { get; set; }
public virtual string Name { get; set; }
}
public class SchoolMap : ClassMap<School>
{
public SchoolMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
Table("school");
}
}
答案 0 :(得分:0)
你应该这样做;
public class Student
{
public virtual int StudentID { get; set; }
public virtual string Name1 { get; set; }
public virtual string Name2 { get; set; }
public virtual List<Class> ClassList { get; set; }
}
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentID ).Column("student_id");
Map(x => x.Name1 ).Column("name_1");
Map(x => x.Name1 ).Column("name_2");
HasMany(x => x.Class).KeyColumn("ClassId")
Table("student");
}
}
public class Class
{
public virtual int ClassID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Student> StudentList { get; set; }
public virtual School School { get; set; }
}
public class ClassMap : ClassMap<Class>
{
public ClassMap ()
{
Id(x => x.ClassID ).Column("class_id");
Map(x => x.Name ).Column("name");
HasMany(x => x.Student).KeyColumn("StudentId")
References(x => x.School)
Table("class");
}
}
public class School
{
public virtual int SchoolID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Class> ClassList { get; set; }
}
public class SchoolMap : ClassMap<School>
{
public SchoolMap ()
{
Id(x => x.SchoolID ).Column("SchoolId");
Map(x => x.Name ).Column("name");
HasMany(x => x.ClassList).KeyColumn("ClassId")
Table("school");
}
}