使用NHibernate从投影中获取子类类型 [英] Get subclass type from projection with NHibernate
问题描述
我正在尝试对名为 Log 的类型进行投影.日志引用了名为 Company 的超类.每个公司类型都与每个子类的表相对应.
I am trying to do a projection on a type called Log. Log references a superclass called Company. Each company type is mapped with table per subclass.
在日志上进行投影时,我能否获得公司的类型? 我目前在每个子类上都有一个Enum属性(未映射),因此可以对Company类型执行切换,但是由于未映射到任何东西,因此无法对其进行投影.
Is it possible for me to get the type of Company when I do a projection on Log? I currently have an Enum property (which is not mapped) on each subclass, so I can perform switches on Company types, but since it is not mapped to anything, I can't do a projection on it.
我已经尝试过Projections.Property("log.Company.class")
,但它不起作用:(
I have tried Projections.Property("log.Company.class")
but that does not work :(
PS:对于这个问题,我找不到很多合适的标签.如果有人想使用更具体的标签,请告诉我.
PS: I couldn't find a lot of appropriate tags for this question. If anyone have an idea for more specific tags, please tell me.
推荐答案
您可以执行以下操作...
You can do the following...
session.CreateCriteria<Log>()
.CreateAlias("Company", "company")
.SetProjection(Projections.Property("company.class"))
但是该投影只能用于过滤和排序; NHibernate不会在结果集中返回System.Type(它返回一个整数,供内部使用).
But that projection can only be used for filtering and ordering; NHibernate will not return the System.Type in the result set (it returns an integer, which is used internally).
如果您需要了解公司的具体类型,则可以执行以下操作:
If what you need is to know the concrete type of the Company, you can do the following:
var logs = session.CreateCriteria<Log>()
.SetFetchMode("Company", FetchMode.Join) //avoid SELECT N+1
.List<Log>()
然后,获取每一行的类型:
And then, to retrieve the type for each row:
foreach (var log in logs)
string companyClassName = session.GetEntityName(log.Company);
这篇关于使用NHibernate从投影中获取子类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!