选项1
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.HashedPassword);
}
}
public class StudentMap : SubclassMap<Student>
{
public StudentMap()
{
Table("Students");
References(x => x.School);
Map(x => x.GPA);
}
}
public class TeacherMap : SubclassMap<Teacher>
...
表
Users (
Id, // PK
Name,
HashedPassword
)
Students (
User_Id, // PK, FK
School_id,
Gpa
)
选项 2
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
DiscriminateSubclassesOnColumn("UserType", "user");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.HashedPassword);
}
}
public class StudentMap : SubclassMap<Student>
{
public StudentMap()
{
DiscriminatorValue("student");
References(x => x.School);
Map(x => x.GPA);
}
}
public class TeacherMap : SubclassMap<Teacher>
...
表
Users (
Id, // PK
UserType,
Name,
HashedPassword
School_id,
Gpa
Classname
)
选项 3
public abstract class UserMap<TUser> : ClassMap<TUser> where TUser : User
{
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.HashedPassword);
}
}
public class StudentMap : UserMap<Student>
{
public StudentMap()
{
Table("Students");
References(x => x.School);
Map(x => x.GPA);
}
}
public class TeacherMap : UserMap<Teacher>
...
表
Students (
Id, // PK
Name,
HashedPassword
School_id,
Gpa
)
Teachers (
Id, // PK
Name,
HashedPassword
School_id,
Classname
)