I'm trying to QueryOver and select a property which returns a private property. I can't explain it well but the example will tell you.
Mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="IWA.Model" assembly="IWA.Model">
<class name="Test" lazy="false" table="Test">
<id name="Id" column="Id" >
<generator class="identity" />
</id>
<bag name="_images" access="field" table="Image" cascade="none">
<key column="IdTest" />
<one-to-many class="Image" />
</bag>
</class>
</hibernate-mapping>
Class:
public class Test : IEntity<int>
{
private readonly IList<Image> _images;
public Test()
{
_images = new List<Image>();
}
public int Id { get; set; }
public Image Image => _images.FirstOrDefault();
}
QueryOver:
TestDto r = null;
Session.QueryOver<Test>()
.Where(x => x.Id == _id)
.Select(
Projections.Property<Test>(x => x.Id).WithAlias(() => r.Id),
Projections.Property<Test>(x => x.Image).WithAlias(() => r.Image)
) .TransformUsing(Transformers.AliasToBean<TestDto>())
.SingleOrDefault<TestDto>();
When I try this query I get 'Could not resolve property'. Querying without projections works fine.
Any idea what I am doing wrong ??
NHibernate cannot resolve an un-mapped, runtime computed property such as Image
, and thus it cannot project it. You can only use mapped properties in projections.
For achieving your projection, you have to join on your Image
table and only Take(1)
. See this for an example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.