我的QueryOver出现问题,我不明白为什么。我的查询返回viewmodel对象ContactInfo。我在Employee属性上收到此错误:“找不到属性的setter”。如何填写ContactInfo中的Employee属性?我做错了什么?
ViewModel对象:
public class ContactInfo
{
public EmployeeInfo Employee { get; set; }
public string Email { get; set; }
public string InternalTelephone { get; set; }
}
查询
public override ContactInfo Execute()
{
ContactInfo r = null;
EmployeeInfo ei = null;
var result = Session.QueryOver<Job>()
.JoinAlias(j => j.EmployeeInfo, () => ei)
.Where(j => j.EmployeeInfo.Id == _employeeId)
.Select(
Projections.Property<Job>(j => ei.Id).WithAlias(() => r.Employee.Id),
Projections.Property<Job>(j => ei.FirstName).WithAlias(() => r.Employee.FirstName),
Projections.Property<Job>(j => ei.LastName).WithAlias(() => r.Employee.LastName),
Projections.Property<Job>(j => ei.ReferenceCode).WithAlias(() => r.Employee.ReferenceCode),
Projections.Property<Job>(j => j.Telefoon).WithAlias(() => r.InternalTelephone)
)
.TransformUsing(Transformers.AliasToBean<ContactInfo>())
.Take(1)
.SingleOrDefault<ContactInfo>();
var email = Session.QueryOver<Employee>()
.Where(e => e.Id == _employeeId)
.Select(e => e.Email)
.SingleOrDefault<string>();
result.Email = email;
return result;
}
}
答案 0 :(得分:1)
我们可以做的是使用与默认不同的结果转换器,例如DeepTransformer。
在这种情况下,查询必须使用与DTO模型类似的别名。因此,如果我们的域属性ei.FirstName
属于 JoinAlias - j.EmployeeInfo
,则别名必须反映DTO ContactInfo - "EmployeeInfo.FirstName"
.Select(
Projections.Property<Job>(j => ei.FirstName) // the mapped domain model
.As("EmployeeInfo.FirstName"), // the path in DTO/view model
...
)
.TransformUsing(DeepTransformern<ContactInfo>()) // the DTO
现在,路径&#34; EmployeeInfo.FirstName&#34; 将用于将Employee
填充为属性 EmployeeInfo
及其属性 FirstName
此结果转换器
DeepTransformer将使用Alias来构建参考树。可用于many-to-one
... 的引用/ IDictionary
(但不适用于集合)
注意:.Is()
方法来自某些扩展程序,可以替换为with != null
,如
public static partial class Ext
{
public static bool Is(this object value)
{
return value != null;
}
....