在[Fluent] Nhibrenate中的继承类上调用存储程序,而无需歧视器

发布于 2025-01-28 18:54:43 字数 1117 浏览 5 评论 0原文

我有一个有效的工程,并带有一些继承的表,

class Base { int BaseId }
class Derived : Base {}
class Derived2 : Base {}

class BaseMap : ClassMap<Base> { ID BaseId ); }
class DerivedMap : SubclassMap<Derived> { Id( BaseId ); }
class DerivedMap2 : SubclassMap<Derived2> {Id( BaseId ); }

这些表没有专用的“歧视器”列,Nhibernate只是将其弄清楚。

我注意到它运行的SQL查询,即使我专门请求base entity,它将加入所有子类,添加case语句,带有命名<代码> clazz ,并使用它返回带有可用的扩展对象的代理(因此 it 知道是否存在'derived2',只会给我这个)。这一切都很好...

问题

我有一个现有的存储过程,该过程从base中返回记录。我正在尝试通过createSqlquery喜欢这样的来调用它:

Session.CreateSQLQuery( "exec SP_GetAllFromBase" )
       .AddEntity( nameof( Base ) )
       .List<Base>();

但是,当我尝试将其映射为base的实体类型时,我会得到indexoutofrange异常,因为NHibernate试图在返回的结果集中找到clazz属性(就像它添加到所有自己的查询中一样...)。但是,存储的过程只是从base返回行,而不是将特殊案例语句添加到最后。

我有没有办法告诉createSqlquery 具体需要返回base实例而不是derived

I have a working Fluent project with some inherited tables

class Base { int BaseId }
class Derived : Base {}
class Derived2 : Base {}

class BaseMap : ClassMap<Base> { ID BaseId ); }
class DerivedMap : SubclassMap<Derived> { Id( BaseId ); }
class DerivedMap2 : SubclassMap<Derived2> {Id( BaseId ); }

These tables do not have a dedicated "discriminator" column, NHibernate just figures it out.

I've noticed that the SQL queries it runs, even if I specifically request a Base entity, it will join to all subclasses, add a CASE statement with a parameter named clazz, and use that to return a proxy with the extended objects available (so it knows if there is a 'derived2' in existence and will just give me that). This all works fine...

Problem

I have an existing stored procedure that returns records from Base. I'm trying to call this via CreateSqlQuery like so:

Session.CreateSQLQuery( "exec SP_GetAllFromBase" )
       .AddEntity( nameof( Base ) )
       .List<Base>();

But when I try to map it an entity type of Base, I get an IndexOutOfRange exception as NHibernate tries to find a clazz property in the returned result set (just like it adds onto all its own queries...). But the stored procedure is just returning rows from Base and not adding a special case statement to the end.

Is there is way I can tell CreateSqlQuery that it specifically needs to return a Base instance and not Derived?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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