Hello,
I have a User which can have many Emails. This is mapped through a List collection (exposed by IEnumerable Emails on the User). For each User one of the Emails will be the Primary one ("Boolean IsPrimary" property on Email).
How can I get the primary Email from User without NHibernate loads every email for the User ?
I have the following two entities, with a corresponding table for each
public class User
{
private readonly IList<Email> _emails;
public virtual int Id { get; private set; }
public virtual IEnumerable<Email> Emails { get { return _emails; } }
// public virtual Email PrimaryEmail { get; set; } - Possible somehow ?
}
public class Email
{
public virtual int Id { get; private set; }
public virtual String Address { get; set; }
public virtual Boolean IsPrimary { get; set; }
public virtual User User { get; set; }
}
Can I map a "Email PrimaryEmail" property etc. on the User to the Email which have "IsPrimary=1" set somehow ? Maybe using a Sql Formula ? a View ? a One-To-One relationship ? or another way ?
It should be possible to change the primary email to be one of the other emails, so i would like to keep them all in 1 table and just change the IsPrimary property.
Using a Sql Formula, is it be possible to keep the "PrimaryEmail" property on the User up-to-date, if I set the IsPrimary property on the current primary email to false, and then afterwards set the PrimaryEmail property to the email which should be the new primary email and set IsPrimary to true ? Will NHibernate track changes on the "old/current" primary Email loaded by the Sql Formula ? What about the 1 level cache and the 2 level cache when using SqlFormula ?
I dont know if it could work by using a View ? Then i guess the Email could be mapped like a Component ? Will it work when updating the Email data when loaded from the View ?
Is there a better way ?
As I have a bi-directional relationship between User and Email I could in many cases of course query the primary Email and then use the "User" property on the Email to get the User (instead of the other way around - going from User to the primary Email)
Hope someone can help ?