I was wondering if someone could help me understand RavenDB
transformations as I cant seem to get them working correctly. I tried
following Ayende's post
http://ayende.com/blog/4661/ravendb-live-projections-or-how-to-do-joins-in-a-non-relational-database
but I am not able to apply it to my situation. I have two models, a
UserModel and an UserAddressModel. The code is as follows
public class UserModel
{
#region Properties
public string Id { get; set; }
public string AccountId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime InsertDate { get; set; }
#endregion
}
and
public class UserAddressModel
{
public string Id { get; set; }
public string AccountId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
public DateTime InsertDate { get; set; }
}
I have my RavenDB Index set up like such.
public class UserDashboard_ByName :
AbstractIndexCreationTask<UserModel>
{
public UserDashboard_ByName()
{
Map = users => from user in users
select new { user.UserName,
user.AccountId };
TransformResults =
(database, users) => from user in users
let useraddress =
database.Load<UserAddressModel>(user.AccountId)
select new
{ FirstName =
user.FirstName,
LastName =
user.LastName,
Address1 =
useraddress.Address1,
Address2 =
useraddress.Address2
};
}
}
and I am calling it with the following code
using (var session = DataDocumentStore.Instance.OpenSession())
{
//Get the AccountId
var userDashboard =
(from user in session.Query<UserModel,
UserDashboard_ByName>()
where user.UserName == userName
select user).SingleOrDefault();
}
When I call the index, it returns a UserModel type, not the anonymous
type that I am expecting. Also, I do not understand how I can call
let useraddress = database.Load<UserAddressModel>(user.AccountId)
when there is no specific relationship that has been specified in code
or anywhere else.
When I view the index in the RavenDB studio, I am getting null for all
of the address fields as well.
Maybe someone can explain to me how I should be joining data in
RavenDB? Some expanded documentation or a nudge in the right direction
to understanding this better would be greatly appreciated. Thanks in
advance.