开头在 Linq to NHibernate 查询中不起作用
在我的存储库中,我有:
public IQueryable<ICustomer> GetByAddress(string address)
{
return from c in GetSession().Linq<ICustomer>()
where c.Address.StartsWith(address)
select c;
}
我希望它生成的 SQL 本质上是:
SELECT *
FROM Customer
WHERE address LIKE @address + '%'
但是,每当我这样做时,
var customers = myRepository.GetByAddress("123 Main Street");
我都会收到 NullReferenceException:
<块引用>System.NullReferenceException 未处理
Message="未将对象引用设置为对象的实例。"
来源=“NHibernate”
堆栈跟踪:
在 NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName)
在 NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(String propertyName, ICriteria subcriteria)
在 NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria,字符串 propertyName)
剪断...
我已经剪掉了异常的其余部分,因为它非常冗长,如果有帮助,我可以添加它。
为什么这不起作用,而完全相同的查询 except with 则
where c.Address == address
工作得很好。
有什么想法吗?
编辑
我正在使用 FluentNHibernate (其中 Id 是 GUID,Address 是字符串):
public class CustomerMap: ClassMap<ICustomer>
{
public JobMap()
{
Id(x => x.Id);
...snip...
Map(x => x.Address).Not.Nullable();
}
}
In my repository I have:
public IQueryable<ICustomer> GetByAddress(string address)
{
return from c in GetSession().Linq<ICustomer>()
where c.Address.StartsWith(address)
select c;
}
The SQL I'd like it to generate is essentially:
SELECT *
FROM Customer
WHERE address LIKE @address + '%'
However, whenever I do
var customers = myRepository.GetByAddress("123 Main Street");
I get a NullReferenceException:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="NHibernate"
StackTrace:
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(String propertyName, ICriteria subcriteria)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria, String propertyName)
Snip...
I have snipped the rest of the exception because it is quite verbose, if it would be helpful I can add it.
Why does this not work, while the exact same query except with
where c.Address == address
works perfectly fine.
Any ideas?
Edit
I'm using FluentNHibernate (where Id is a GUID and Address is a string):
public class CustomerMap: ClassMap<ICustomer>
{
public JobMap()
{
Id(x => x.Id);
...snip...
Map(x => x.Address).Not.Nullable();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论