Include on Many to One relationship

111 views
Skip to first unread message

Eniep Yrekcaz

unread,
Dec 17, 2012, 3:09:13 PM12/17/12
to rav...@googlegroups.com
If i have an account object
Account
{
     Id = 1
}
and contact objects that reference that account:
Contact
{
     Id = 1,
     AccountId = 1
}
{
     Id = 2,
     AccountId = 1
}
{
     Id = 3,
     AccountId = 1
}
and then want to perform and include, something like:
var ravenAccounts = session.Include<Contact>(x => x.AccountId).Load<Account>(x => "Accounts/" + x.Id); 
It seems as though if I then try to do:
var contact = session.Load<Contact>(account.Id);
it will only return me one contact. 

How can I get back a list of contacts that have AccountId == account.Id?
Thanks.

Matt Johnson

unread,
Dec 17, 2012, 3:16:58 PM12/17/12
to ravendb
Includes won't help you here. You have no reference back to contacts
from the account. You'll need to query:

session.Query<Contact>().Where(x => x.AccountId == 1)

If you had accounts such as:

Account
{
Id = 1,
ContactIds = [1,2,3]
}

then you could do it with a load, using the following syntax:

session.Include<Account, Contact>(x => x.ContactIds).Load<Account>(1)


On Dec 17, 1:09 pm, Eniep Yrekcaz <peinearydevelopm...@gmail.com>
wrote:

Eniep Yrekcaz

unread,
Dec 17, 2012, 4:13:16 PM12/17/12
to rav...@googlegroups.com
Thanks for the help. It seems as though Load is looking for a string, does the second half of your statement need to read .Load("Accounts/" + 1) ?

Matt Johnson

unread,
Dec 17, 2012, 4:30:27 PM12/17/12
to ravendb
No, there is an overload that will take an integer or guid if you want
to use non-string identifiers.

On Dec 17, 2:13 pm, Eniep Yrekcaz <peinearydevelopm...@gmail.com>
wrote:

Eniep Yrekcaz

unread,
Dec 17, 2012, 4:40:38 PM12/17/12
to rav...@googlegroups.com
I'm using 960 and Intellisense won't allow me to pass an int. What version was that overload added?

Matt Johnson

unread,
Dec 17, 2012, 4:56:41 PM12/17/12
to ravendb
It's been there for a LONG time. Added April 17 2011.
https://github.com/ravendb/ravendb/commit/5e002af2adc3c6a9f365e761ce44e3157183dbed

It's part of the Raven.Client.Document.ILoaderWithInclude<T>
interface. It's signature is:

TResult Load<TResult>(ValueType id);

I just verified on 960 and it works fine. Not sure what else you're
missing. Maybe you forgot to specify a type?

On Dec 17, 2:40 pm, Eniep Yrekcaz <peinearydevelopm...@gmail.com>

Matt Johnson

unread,
Dec 17, 2012, 5:11:12 PM12/17/12
to ravendb
Are you using an async session by chance??

On Dec 17, 2:56 pm, Matt Johnson <mj1...@hotmail.com> wrote:
> It's been there for a LONG time.  Added April 17 2011.https://github.com/ravendb/ravendb/commit/5e002af2adc3c6a9f365e761ce4...

Eniep Yrekcaz

unread,
Dec 18, 2012, 1:19:35 PM12/18/12
to rav...@googlegroups.com
I don't believe I'm using an Async session. Another problem I'm having is that Raven is complaining that I'm reaching beyond the 30 transaction limit. If those docs are included in the first load(utilizing include), all subsequent loads(even if they are on those included items get counted towards that 30 max?)

Matt Johnson

unread,
Dec 18, 2012, 3:31:27 PM12/18/12
to ravendb
Ok. I was asking because I found one of the Include overloads missing
from the async session and just submitted a request for it:
https://github.com/ayende/ravendb/pull/197

Regarding transaction limit - that's not a count of how many documents
your are loading or including. It's a count of how many total
requests you have made to the server. When you request multiple
documents at once, whether via Load(ids[]) or via Include(), you can
request as many as you like and they are made in a single request.
That's kinda the point of includes.

Back to your original problem - if you're not using async sessions,
and you can't pass an integer to .Load(), then there is something
seriously wrong. That's a normal use case and has been in raven
stable for over a year. If you want to show some actual code (gist/
pastebin), perhaps we can help you identify the problem.


On Dec 18, 11:19 am, Eniep Yrekcaz <peinearydevelopm...@gmail.com>
wrote:

Eniep Yrekcaz

unread,
Dec 18, 2012, 5:56:13 PM12/18/12
to rav...@googlegroups.com
I'm sorry if I misspoke before. I'm not passing in an int, but I'm trying to pass an IEnumerable<int>. That is not working. I'm not quite sure why:
session.Load<Account>(accountIds.Select(x => "Accounts/" + x)) will work, but 
session.Load<Account>(accountIds) won't work.

Also, I think I understand what you are saying about the transaction limit. Here is where I'm confused...
This is the example on the RavenDB website:
var order = session.Include<Order>(x => x.CustomerId)
    .Load("orders/1234");
 
// this will not require querying the server!
var cust = session.Load<Customer>(order.CustomerId);

And while that second statement doesn't need to query the server, it is still counted as one of the 30 allowed Transactions per session. I was assuming the 30 limit was db calls. Why is this not the case?
Thanks!

Matt Johnson

unread,
Dec 18, 2012, 6:20:26 PM12/18/12
to ravendb
That makes sense about the ids now. There isn't an overload that
takes an array of non-string identifiers. Perhaps one could be added.

The way you're loading is fine, but you could also get "Accounts/" via
the document conventions if you wanted to avoid the string.

Regarding loading from includes - I don't think they shouldn't be
counted towards the transaction limit. Not sure if this is a bug or
my misunderstanding. Anyone else know?


On Dec 18, 3:56 pm, Eniep Yrekcaz <peinearydevelopm...@gmail.com>
wrote:

Oren Eini (Ayende Rahien)

unread,
Dec 18, 2012, 7:54:07 PM12/18/12
to rav...@googlegroups.com
Load that hit the session cache does NOT count.

Matt Johnson

unread,
Dec 19, 2012, 10:30:07 AM12/19/12
to ravendb
In 2.0, yes. But apparently in 1.0 if the include was done in a query
then the load is counted. :(

https://gist.github.com/4337489


On Dec 18, 5:54 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:

Oren Eini (Ayende Rahien)

unread,
Dec 19, 2012, 10:36:43 AM12/19/12
to rav...@googlegroups.com
Oh, sure, forgot about it

Matt Johnson

unread,
Dec 19, 2012, 1:34:05 PM12/19/12
to ravendb
@Enip - I've completed those missing overloads for loading with
multiple non-string ids (for the 2.0 release).

@Oren: https://github.com/ayende/ravendb/pull/204


On Dec 19, 8:36 am, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:
> Oh, sure, forgot about it
>
>
>
>
>
>
>
> On Wednesday, December 19, 2012, Matt Johnson wrote:
> > In 2.0, yes.  But apparently in 1.0 if the include was done in a query
> > then the load is counted.  :(
>
> >https://gist.github.com/4337489
>
> > On Dec 18, 5:54 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com<javascript:;>
>
> > wrote:
> > > Load that hit the session cache does NOT count.
>
> > > On Wed, Dec 19, 2012 at 1:20 AM, Matt Johnson <mj1...@hotmail.com<javascript:;>>
> > wrote:
> > > > That makes sense about the ids now.  There isn't an overload that
> > > > takes an array of non-string identifiers.  Perhaps one could be added.
>
> > > > The way you're loading is fine, but you could also get "Accounts/" via
> > > > the document conventions if you wanted to avoid the string.
>
> > > > Regarding loading from includes - I don't think they shouldn't be
> > > > counted towards the transaction limit.  Not sure if this is a bug or
> > > > my misunderstanding.  Anyone else know?
>
> > > > On Dec 18, 3:56 pm, Eniep Yrekcaz <peinearydevelopm...@gmail.com<javascript:;>

Oren Eini (Ayende Rahien)

unread,
Dec 19, 2012, 3:48:04 PM12/19/12
to rav...@googlegroups.com
Pulled, and thanks

Eniep Yrekcaz

unread,
Dec 19, 2012, 4:49:29 PM12/19/12
to rav...@googlegroups.com
Thank you very much for that addition Matt. I'm really looking forward to 2.0. As far as my Include question, I found a bug in my code which seems to be what was causing the problem. Thanks again for all of your help.
Reply all
Reply to author
Forward
0 new messages