I am working with NHibernate and I get this code below:
public class User
{
public User()
{
public virtual int Id { get; set; }
public virtual IList<UserConfirmation> UserConfirmation { get; set; }
public virtual string Email;
}
public User()
{
UserConfirmation = new List<UserConfirmation>();
}
}
public class UserConfirmation
{
public virtual int Id { get; set; }
public virtual User { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
Map(x => x.Email);
HasMany(x => x.UserConfirmation)
.Inverse()
.Cascade.All();
Table("user");
}
}
public class UserConfirmationMap : ClassMap<UserConfirmation>
{
public UserConfirmationMap()
{
Id(x => x.Id);
References(x => x.User);
Table("user_confirmation");
}
}
But when I try to query over like this:
QueryOver<UserConfirmation>().Where(x => x.User.Email).Take(1).SingleOrDefault()
Than it says I do not have the property User.Email.
How can I solve this problem?
Problem is you are using x => x.User.Email this should be done with another alias, which would be user
UserConfirmation userConfirmationAlias;
User userAlias;
QueryOver<UserConfirmation>(() => userConfirmationAlias)
.joinAlias(() => userConfirmationAlias.User , () => userAlias)
.Where(() => userAlias.Email).Take(1).SingleOrDefault()
something like above should do the trick
I think you want something like this.
QueryOver<User>().Where(x => x.Email).Take(1).SingleOrDefault()
x should be a user already. If it's not I would use the intellisense to see what type it thinks it is.
Try doing:
IQueryOver<UserConfirmation,User> qo = _Session.QueryOver<UserConfirmation,User>().Where(x => x.User.Email).Take(1).SingleOrDefault();
You could use LINQ instead of QueryOver:
Query<UserConfirmation>().Where(x => x.User.Email).Take(1).SingleOrDefault()
BTW, .Take(1).SingleOrDefault()
is probably not needed. If Email
is unique, .SingleOrDefault()
will be enough. Otherwise, you can use .FirstOrDefault()
QueryOver()
.JoinQueryOver(x => x.User)
.Take(1) .SingleOrDefault() - Michel Andrade 2012-04-04 10:54