[RavenDB] Using property other than "Id" for document key.

650 views
Skip to first unread message

Wesley Johnson

unread,
May 19, 2010, 11:13:32 PM5/19/10
to ravendb
I've got an application already built using SQL Express as the backend
and I'm toying around with converting the backend over to Raven DB. My
business/domain objects user a rather redundant way of defining their
keys so my "User" object's key is "UserID". Is it possible to get
Raven to generate it's keys using different names depending on the
object type or at least let me specify the key for each object type
and I'll generate it?

I had a look at FindIdentityProperty, but I'm not exactly sure how to
use it as it doesn't seem to be working the way I attempted. If
FindIdentityProperty is the proper method, an example would be great
and also do I need to set those conventions before initialise() or can
I set them at any time?

Ayende Rahien

unread,
May 19, 2010, 11:52:55 PM5/19/10
to ravendb
Conventions.FindIdProperty = > prop => {
   if(prop.DeclaringType == typeof(User) && prop.Name == "UserId")
        return true;
    return false;
};

Or something like:

Conventions.FindIdProperty = > prop => prop.DeclaringType.Name + "Id"  == prop.Name;

Wesley Johnson

unread,
May 20, 2010, 12:47:04 AM5/20/10
to ravendb
Okay, I tried this:

DocumentStore.Conventions.FindIdentityProperty(prop =>
prop.DeclaringType.Name + "ID" == propertyName);

and I'm getting this error:

Cannot convert lambda expression to type
'System.Reflection.PropertyInfo' because it is not a delegate type.

I trying doing some googling, but I'm really out of my element when it
comes to Linq/Lambda, etc. Thanks for your help so far, any more would
be greatly appreciated!

On May 19, 11:52 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Conventions.FindIdProperty = > prop => {
>    if(prop.DeclaringType == typeof(User) && prop.Name == "UserId")
>         return true;
>     return false;
>
> };
>
> Or something like:
>
> Conventions.FindIdProperty = > prop => prop.DeclaringType.Name + "Id"  ==
> prop.Name;
>
> On Thu, May 20, 2010 at 4:13 AM, Wesley Johnson
> <johnson.wesle...@gmail.com>wrote:

Matt Warren

unread,
May 20, 2010, 2:29:25 AM5/20/10
to ravendb
I think you want to do this instead

DocumentStore.Conventions.FindIdentityProperty =
prop => prop.DeclaringType.Name + "ID" == propertyName);

FindPropertyInfo is a setter not a function.

Wesley Johnson

unread,
May 20, 2010, 10:43:21 AM5/20/10
to ravendb
Ah, thanks Matt. I see what I was doing wrong. I'm now having an issue
where the property I defined as an ID is now no longer saved in the
document's content, but I didn't have much time to fiddle around with
that before I had get back to work projects. I'll give it a go again
tonight.

Thanks!
> > > > I set them at any time?- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
May 20, 2010, 11:23:22 AM5/20/10
to ravendb
That is intentional, the ID isn't stored with the document, it is _about_ the document.

Wesley Johnson

unread,
May 20, 2010, 12:10:45 PM5/20/10
to ravendb
Thanks, Ayende. I assumed it was intentional, I just wasn't sure how
I'm going to work around it. When I'm querying or loading the document
back out of RavenDB and it's mapping to a instance of my User object,
I need to then populate the aforementioned UserID field with the ID of
the document. I'm hoping that since FindIdentityProperty was set to
that RavenDB knows the UserID property is in indeed my "identity" that
it would map this for me, but I was running into issues with that
field being defaulted in my tests.

Admittedly I hadn't had a chance to really dig, it's possible I was
doing something wrong. I'm a bit rusty with my .NET in general and
LINQ may as well foregin language to me. I'll have a go later this
evening and post back when I'm convinced I can't figure it out on my
own.

Great work on this, btw. I'm enjoying it thus far. :)

On May 20, 11:23 am, Ayende Rahien <aye...@ayende.com> wrote:
> That is intentional, the ID isn't stored with the document, it is _about_
> the document.
>
> On Thu, May 20, 2010 at 3:43 PM, Wesley Johnson
> <johnson.wesle...@gmail.com>wrote:
> > > - Show quoted text -- Hide quoted text -

Ayende Rahien

unread,
May 20, 2010, 12:20:23 PM5/20/10
to ravendb
You shouldn't be able to tell that the ID isn't stored with the document from the client code.
I'll write a test for this.

Wesley Johnson

unread,
May 20, 2010, 8:59:11 PM5/20/10
to ravendb
Everything seems good now. I forgot that I was using an index and
querying for objects based on my "UserID" field before I told RavenDB
it was by actual "Id". Once I fixed the latter part, I forgot to
change the method from retrieving a single object from a query to the
session.load<t> method. Because the "UserID" was no longer part of the
document, but now the "key" - RavenDB wasn't returning any objects and
I was confused.

All is working great now. Thanks for everyone's help!

On May 20, 12:20 pm, Ayende Rahien <aye...@ayende.com> wrote:
> You shouldn't be able to tell that the ID isn't stored with the document
> from the client code.
> I'll write a test for this.
>
> On Thu, May 20, 2010 at 5:10 PM, Wesley Johnson
> <johnson.wesle...@gmail.com>wrote:

Ayende Rahien

unread,
May 21, 2010, 3:36:46 AM5/21/10
to rav...@googlegroups.com
Okay, cool
Reply all
Reply to author
Forward
0 new messages