Casting NHibernate Lazy loaded objects (Proxy problem)
I have a class Client like that:
public class Client{
public Person Pers { get; set; }
}
And I have 2 Person´s ch开发者_JAVA百科ild class :
public class PersonType1 : Person {...}
public class PersonType2 : Person {...}
Now I loaded a client... And I need to get the PersonType1 or PersonType2 attributes ..
I tried that:
var _pj = ((PersonType1 ) _client.Pers);
But it does not work, because the _client.Pers type is a Proxy (Lazy load true) ...
Is there a way to do that? I have several attributes in each PersonType, so It is not a good idea to create a virtual/override for each attribute (Person->PersonType1) ...
Thanks
You could try to eagerly fetch the Pers
property:
var client = session
.CreateCriteria<Client>()
.CreateCriteria("Pers", JoinType.LeftOuterJoin)
.Add(Expression.IdEq(1))
.UniqueResult<Client>();
var pj = (PersonType1)client.Pers;
If you use NH in the server, and move the objects to the client, you can't use lazy load. What NH knows (in the server) doesn't help the client, which has neither Session nor knowledge how to fetch the extra data when needed.
精彩评论