问题
I added some calculated read-only properties to my class and it's now throwing a QueryException: could not resolve property.
Here is my class (fake calculations right now):
public class IncompleteApplication : DealerBase
{
public virtual string Content { get; set; }
public virtual string LegalBusinessName
{
get
{
return "Leg";
}
}
public virtual string DbaName
{
get
{
return "Dba";
}
}
}
Mapping:
public class IncompleteApplicationMap : DealerBaseMap<IncompleteApplication>
{
public IncompleteApplicationMap()
{
Schema("Dealer");
Table("XmlSerialization");
Map(app => app.Content);
}
}
And calling code:
data.GridDataItems = (from app in _Repository.GetAll()
select new GridData.GridDataItem()
{
ID = app.Id,
SubmittedDate = app.LastUpdated,
UserName = app.User.UserName,
LegalBusinessName = app.LegalBusinessName,
DbaName = app.DbaName
}).ToArray();
_Repository.GetAll() returns an IQueryable. When I add a .ToList() after GetAll() the code runs just fine (although I get a Select N + 1 situation).
Thanks for any help!
回答1:
You should map your two read-only properties with nhibernate and use a formula to provide their values while querying. I don't know fluent nh very well, but a standard xml mapping for your properties would look something like:
<property name="DbaName" access="readonly" insert="false" update="false" type="String" formula="(SELECT 'Dba')" />
回答2:
nHibernate is generating a sql statement to execute on the server. Calculated fields don't exist in the db so you can't use them in your nHibernate query.
What you can do is execute the query to that meets all your criteria except those calculate fields, then after .ToArray() using linq on those object.
data.GridDataItems = (from app in _Repository.GetAll()
select new GridData.GridDataItem()
{
ID = app.Id,
SubmittedDate = app.LastUpdated,
UserName = app.User.UserName,
}).ToArray().Where(i=> i.LegalBusinessName = app.LegalBusinessName && i.DbaName = app.DbaName);
来源:https://stackoverflow.com/questions/3366049/adding-calculated-properties-to-class-throws-nhibernate-error-when-using-linq-to