我对nHibernate有些陌生,特别流利,我在映射类A
扩展类B
的关系时遇到了麻烦,但不保证给定A
1}}也是B
。这里有一个关于结构到目前为止的概念(并且可以接受建议):
public class User
{
public virtual Guid Id;
public virtual string UserName;
public virtual string HashedPassword;
//...
}
public class Student : User
{
public virtual Guid UserId;
public virtual School School;
public virtual float GPA;
}
public class Teacher : User
{
public virtual Guid UserId;
public virtual School School;
public virtual string ClassName;
}
所以我想拥有可以使用的Teacher
和Student
个对象,但我也希望能够跟踪它们以进行成员资格验证(因此User
类)。
所以有两个问题:
这是一种合理的方式来做我想做的事吗?
如果是这样,如何使用Fluent nHibernate进行映射?
答案 0 :(得分: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
)
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
)
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
)