简体   繁体   English

NHibernate QueryOver无法解析属性

[英]NHibernate QueryOver Could not resolve property

I'm trying to QueryOver and select a property which returns a private property. 我正在尝试QueryOver并选择一个返回私有属性的属性。 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: 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. NHibernate无法解析未映射的运行时计算属性,例如Image ,因此无法对其进行投影。 You can only use mapped properties in projections. 您只能在投影中使用映射的属性。

For achieving your projection, you have to join on your Image table and only Take(1) . 为了实现投影,您必须加入Image表并且仅加入Take(1) See this for an example. 是一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM