NHibernate Criteria Find by Id
I have 2 entities:
public class Authority : Entity
{
[NotNull, NotEmpty]
public virtual string Name { get; set; }
[NotNull]
public virtual AuthorityType Type { get; set; }
}
public class AuthorityType : Entity
{
[NotNull, NotEmpty]
public virtual string Name { get; set; }
public virtual string Descr开发者_StackOverflowiption { get; set; }
}
Now I wish to find all authorities from repository by type. I tried doing it like this:
public IList<Authority> GetAuthoritiesByType(int id)
{
ICriteria criteria = Session.CreateCriteria(typeof (Authority));
criteria.Add(Restrictions.Eq("Type.Id", id));
IList<Authority> authorities = criteria.List<Authority>();
return authorities;
}
However, I'm getting an error that something is wrong with the SQL ("could not execute query". The innerexception is following: {"Invalid column name 'TypeFk'.\r\nInvalid column name 'TypeFk'."}
Any advice ? Any other approach ?
Best wishes, Andrew
It looks like your mapping file for the Authority entity is associating the Type
property to a column named TypeFk
in whatever table that the Authority entity is mapped to. For some reason, that column is not there.
It might be helpful to see your mapping files too.
精彩评论