Andrey Shchekin
Andrey Shchekin

Reputation: 21599

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 matches 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'.

Upvotes: 1

Views: 1008

Answers (3)

Andrey Shchekin
Andrey Shchekin

Reputation: 21599

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.

Upvotes: 1

DanP
DanP

Reputation: 6478

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.

Upvotes: 0

DanP
DanP

Reputation: 6478

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.

Upvotes: 0

Related Questions