Anton K
Anton K

Reputation: 561

Computed Foreign Key Property

I have a class named Password:

public class Password {
    public virtual int PasswordID { get; set; }
    public virtual int UserID { get; set; }
    public virtual User User { get; set; }
}

The property User is retrieved automatically from UserID property on load:

public PasswordMap() {
    Table("Password");
    LazyLoad();
    Id(x => x.PasswordID).GeneratedBy.Identity().Column("PasswordID");
    Map(x => x.UserID).Column("UserID");
    HasOne(x => x.User);
}

I want the User property to change automatically on UserID change.

var pass = session.Load<Password>(1);
pass.UserID = 5; // User must become null or fetched Entity with ID = 5

I know there is such a feature in RIA Services. But I couldn't find it in NHibernate.

Any help appreciated.

Upvotes: 0

Views: 68

Answers (1)

MichaC
MichaC

Reputation: 13390

Simply no, and why?? There is absolutely no reason for an behavior like that.

Use session.Load or session.Get to get an object by ID.

You would even cause issues by changing the UserId, the automatic change tracking of nh might recognize that and will update the user with the new ID with the properties of the user with the old id...

Upvotes: 1

Related Questions