Lets say we have the following model :
public class ReadingOrder
{
public virtual int Id { get; set; }
public virtual Order Order { get; set; }
}
Mapping:
Table("db_ReadingOrder");
Id(o => o.Id).Column("Id").GeneratedBy.Identity();
References(o => o.Order, "OrderId");
I want to get the ReadingOrder
which has the orderId
equal with 1 (eg).
But when I try a FirstOrDefault
, the query returns null :
var readingO = _repositoryFactory.GetRepository<ReadingOrder>().FirstOrDefault(xz => xz.Order.Id == 1);
If I get all of them and after apply a FirstOrDefault
works, but its stupid:
var readingOrderList1 = _repositoryFactory.GetRepository<ReadingOrder>()
.GetAll().FirstOrDefault(xz => xz.Order.Id == 1);
The method from repository has the following format:
public T FirstOrDefault(Expression<Func<T, bool>> predicate)
{
return _session.Query<T>().FirstOrDefault(predicate);
}
easy stuff, but not working. If I go for a normal property, like Id
, all works as expected.
Also, if I get the generated query from log and put it in sqlite, it runs successfully and the reading order is returned. Is there a bug in NHibernate? Is a mapping problem? Or is it a problem with SQLite?
Ok, finally I found the problem : the name of the foreign key column "OrderId". Nhibernate looks for "Hibernate.Order" in this case, I don`d know why, but after I changed the name of the column, the item is now retrieved from db. Thank you all for your answers! I gave the bounty to user Syed Farjad Zia Zaidi, because he helped me to isolate the problem. It was clear that was an Nhibernate issue, so thank you again.