开头在 Linq to NHibernate 查询中不起作用

发布于 2024-09-11 10:26:24 字数 1531 浏览 8 评论 0原文

在我的存储库中,我有:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文