开发者

NHibernate: projecting a subclass type of an entity

开发者 https://www.devze.com 2023-01-10 06:37 出处:网络
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.

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

精彩评论

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