I'm building a webapplication with .Net Core using web api, angular 2 and NHibernate. I have the following tables in my database:
Todo
ID
Name
Priority
Priority
ID
Name
And the following mapping for these tables:
[Class(NameType = typeof(Todo), Table = "Todo")]
public class Todo
{
[ID(-2, Name = "ID")]
[Generator(-1, Class = "native")]
public virtual long ID { get; set; }
[Property]
public virtual string Name { get; set; }
[ManyToOne]
public virtual Priority Priority { get; set; }
}
[Class (NameType = typeof(Priority), Table = "Priority")]
public class Priority
{
[ID(-2, Name = "ID")]
[Generator(-1, Class = "native")]
public virtual long ID { get; set; }
[Property]
public virtual string Name { get; set; }
}
I also have the following DTO that I want to create a list of and send it to the client in json: For the purpose of this example I have stripped some other properties from it.
public class TodoDTO
{
public long ID { get; set; }
public string Name { get; set; }
public Priority Priority { get; set; }
}
When I run the query below:
var session = SessionFactoryConfigurationBase.GetSessionFactory().GetCurrentSession();
var query = session.QueryOver<Todo>();
TodoDTO todoSummary = null;
query.SelectList(list => list
.Select(t => t.ID).WithAlias(() => todoSummary.ID)
.Select(t => t.Name).WithAlias(() => todoSummary.Name)
.Select(t => t.Priority).WithAlias(() => todoSummary.Priority))
.TransformUsing(Transformers.AliasToBean<TodoDTO>());
the resulting json doesn't show the ID and Name properties of Priority but it shows the following:
[{
"id":1,
"name":"TEST",
"priority":
{
"__interceptor":
{
"persistentClass":"Todo, ApplicationName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"identifier":2,
"isUninitialized":true,
"unwrap":false,
"session":null,
"entityName": "Priority",
"isReadOnlySettingAvailable":false
}
}
}]
Why is it not showing the ID and Name properties but the class definition instead?
When i create a separate list from the above query I get the result I expected in the fist place but that seems rather cumbersome.
--------------------- EDIT -------------------------
As requested the code that does give me the result i expect:
public IList<Todo> GetTodos()
{
var session = SessionFactoryConfigurationBase.GetSessionFactory().GetCurrentSession();
var query = session.QueryOver<Todo>()
.Fetch(t => t.Priority).Eager
.List<Todo>();
if(!query.Any())
{
return null;
}
var result = (
from t in query
select new TodoDTO
{
ID = t.ID,
Name = t.Name,
Priority = t.Priority
}
).ToList();
return result;
}
The result is return to the client with the following code:
public JsonResult GetTodos()
{
var todos = GetTodos();
return new JsonResult(todos);
}
List()method on the resultingquery.