开发者

How to have two mapping for the same table in Fluent nHibernate?

开发者 https://www.devze.com 2023-01-10 19:12 出处:网络
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1) NOT NULL, UserName varchar(128) NOT NULL,

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 n开发者_高级运维eed 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号