如何在 Fluent nHibernate 中对同一个表进行两个映射?

发布于 2024-09-13 21:01:42 字数 1211 浏览 2 评论 0原文

我有一个像这样定义的 User 表:

CREATE TABLE Users(
    UserId int IDENTITY(1,1) NOT NULL,
    UserName varchar(128) NOT NULL,
    Name nvarchar(200) NOT NULL,
    Password binary(64) NOT NULL,
    PasswordSalt binary(16) NOT NULL
)

我试图有两个映射到该表的类:

  • 第一个名为 User 的对象没有 Password 和 PasswordSalt 属性。
  • 第二个对象称为SecurityUser,继承自User,并定义Password 和PasswordSalt 属性。

这背后的想法是,SecurityUser 是一个内部对象,需要中间服务来修改密码。这是为了避免每次我需要查询用户时返回密码和盐。

我所说的 User 类是一个不提供任何用户敏感信息的安全对象。

现在,我已经定义了两个映射:

public class UserMap : ClassMap<User>
{
    protected UserMap()
    {
        Id(x => x.Id);
        Map(x => x.UserName);
        Map(x => x.Name);
    }
}

public class SecurityUserMap : SubclassMap<SecurityUser>
{
    protected SecurityUserMap()
    {
        Map(x => x.Password);
        Map(x => x.PasswordSalt);
        Table("Users");
    }
}

问题是 nHibernate 创建了一个名为 SecurityUser 的表。我尝试使用 Table("Users") 函数来指定同一个表,但随后我得到了无效的 nhibernate 映射。

我怎样才能实现我想要做的事情?或者有其他方法吗?

I have a User table defined like this:

CREATE TABLE Users(
    UserId int IDENTITY(1,1) NOT NULL,
    UserName varchar(128) NOT NULL,
    Name nvarchar(200) NOT NULL,
    Password binary(64) NOT NULL,
    PasswordSalt binary(16) NOT NULL
)

I'm trying to have two class that map to this table:

  • The first object, called User has no Password and PasswordSalt properties.
  • The second object, called SecurityUser, inherits from User and defines the Password and PasswordSalt properties.

The idea behind this is that SecurityUser is a internal object that require a intermediate service to modify the password. This is needed to avoid returning the password and salt everytime I need to query a user.

The User class is, what I call, a safe object that doesn't provide any user sensitive information.

Right now, I have defined two map:

public class UserMap : ClassMap<User>
{
    protected UserMap()
    {
        Id(x => x.Id);
        Map(x => x.UserName);
        Map(x => x.Name);
    }
}

and

public class SecurityUserMap : SubclassMap<SecurityUser>
{
    protected SecurityUserMap()
    {
        Map(x => x.Password);
        Map(x => x.PasswordSalt);
        Table("Users");
    }
}

The problem is that nHibernate creates a table called SecurityUser. I tried using the Table("Users") function to specify the same table, but I then get a invalid nhibernate mapping.

How can I achieve what I am trying to do? Or is there is an alternate approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2024-09-20 21:01:42

NHibernate 不知道何时保存用户以及何时保存安全用户。您需要数据库中的某些内容来告诉 NHibernate 记录何时是用户以及何时是安全用户。
为了告诉您如何做到这一点,我需要知道为什么“需要这样做以避免每次需要查询用户时都返回密码和盐。”?当原因是性能时,您可能无法衡量差异。如果您使用 User 类来报告场景,则可以更好地使用投影类来选择报告查询的结果,而不是映射实体。

NHibernate does not know when to save a User and when to save a security user. You need something in your database to tell NHibernate when a record is a user, and when it is a security user.
To tell you how do to that, I need to know why "This is needed to avoid returning the password and salt everytime I need to query a user."? When the reason is performance, You can probably not measure the difference. If you use the User class for reporting scenario's, you can better use a projection class to select the result of the reporting queries to, than a mapped entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文