I have a problem with NHibenate. When I run queryover, I get an error "could not resolve property: User.Name of: MegaOnlineChat.Core.Entities.Message".What am I doing wrong?
Entity objects
public class EntityObject<TId>
{
public virtual Int32 Id { get; set; }
}
public class User:EntityObject<Int32>
{
public virtual String Name { get; set; }
public virtual String Password { get; set; }
public virtual Boolean Admin { get; set; }
public virtual IList<Message> Messages { get; set; }
}
public class Message:EntityObject<Int32>
{
public virtual String Text { get; set; }
public virtual User User { get; set; }
public virtual DateTime Date{ get; set; }
}
Mapping
public class UserMapping:ClassMap<User>
{
public UserMapping()
{
Table("Users");
Id(m => m.Id).GeneratedBy.Native();
Map(m => m.Name).Unique().Not.Nullable();
Map(m => m.Admin).Not.Nullable();
Map(m => m.Password).Not.Nullable();
HasMany(m => m.Messages).KeyColumn("User_id");
}
}
public class MessageMapping:ClassMap<Message>
{
public MessageMapping()
{
Table("Messages");
Id(m => m.Id).GeneratedBy.Native();
Map(m => m.Text).Not.Nullable();
Map(m => m.Date).Not.Nullable();
References(m => m.User, "User_id").Not.Nullable();
}
}
QueryOver
NHibernateSession.QueryOver<Message>().Where(x => x.User.Name == name).List<Message>().Last();
QueryOver is not the same as the Linq query provider. It would be easier to do:
NHibernateSession.Query<Message>().Where(x => x.User.Name == name).ToList();
But if you want to use QueryOver, you will need to explicitly join the relationships you want to navigate:
NHibernateSession.QueryOver<Message>()
.JoinQueryOver(x => x.User) // navigate to the user on the relationship
.Where(u => u.Name == name) // this restriction is on the User now
.List();
you could also do this using an alias for user
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With