开发者

NHibernate: projecting a subclass type of an entity

How do I query a class of a specific entity in NHibernate?

I basically want a projection that returns a System.Type of each row that matche开发者_开发技巧s criteria.

I have looked at Get subclass type from projection with NHibernate however when I create Projections.Property("alias.class") or Projections.Property("class"), I always get could not resolve property 'class'.


Projections.Property("class") is possible and it works, but only if the class has a discriminator.

I got an answer from person on my team (Denis Bykov).
Unfortunately I had hard time making him answer here so I can award him reputation.


I don't think this is possible using NHibernate directly; but consider adding the following to your base entity class (assuming you have one):

protected virtual Type GetTypeUnproxied() {
    return GetType();
} 

After you have queried your entities, you can interrogate this property to return the actual CLR type of the entity.


If you can't get access to the type through NHibernate for projection purposes, perhaps you can store the System.Type in a field using a custom user type. This should give you the exact functionality you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜