1

我对 nHibernate 有点陌生,特别流利,而且我在映射类A扩展类的关系时遇到了麻烦B,但不能保证给定A的也是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;
}

所以我想拥有可以使用TeacherStudent对象,但我也希望能够跟踪它们以进行成员身份验证(因此是User基类)。

所以两个问题:

  1. 这是做我想做的事情的合理方法吗?

  2. 如果是这样,如何使用 Fluent nHibernate 进行映射?

4

1 回答 1

1

选项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
)
于 2013-07-19T19:46:59.453 回答