English 中文(简体)
RavenDB: Id Generation For Sub-Documents
原标题:

I m trying migrating an existing web application to use RavenDB.

I currently have pages in my web application which allow you to view Categories, SubCategories and Resources based on an id in the querystring.

However I notice that RavenDB generates ids for aggregate roots, but not for child entities.

I don t think subcategory is an aggregate root (a Category has SubCategories), so am making it a sub-document of my Category document.

Am I wrong to make it a sub-document as I m accessing it directly by its id passed in on the querystring? But if not, how should I access individual SubCategories as RavenDB does not seem to generate ids for entities that are not aggregate roots?

最佳回答

There s a long but interesting discussion over on the Raven mailing list about this exact situation.

The short answer is that Raven isn t designed to do this, only root entities get an id, everything else is treated as a value type. But you can implement it yourself, see the code sample at the end of the thread for info.

问题回答

I ran into this problem but wasn t comfortable with letting the documents generate the ID s as I didn t feel it was thread safe, particularly for web based environments.

Eventually I decided to let the server generate the id s for me using a the GenerateDocumentKey method like so:

using (var session = Store.OpenSession())
{
    if(category.SubCategories != null)
    {
       var newSubCategories = data.BankAccounts.Where(x => string.IsNullOrEmpty(x.Id));
        foreach (var sc in newSubCategories)
            sc.Id = session.Advanced.Conventions.GenerateDocumentKey(sc);    
    }

    session.Store(data);
    session.SaveChanges();
}

This way I m allowing the database to generate the child Id s and can ensure that I won t have to cater for race conditions etc in the actual class itself.





相关问题
NO-SQL reliable for small business app?

I m deciding between go for a NON-SQL engine or a regular SQL one for a document managment system for small bussines. I have experience with firebird/sql server and found a good track of reliability (...

CouchDB View, Map, Index, and Sequence

I think read somewhere that when a View is requested the "map" is only run across documents that have been added since the last time it was requested? How is this determined? I thought I saw something ...

Cassandra Vs Amazon SimpleDB

I m working on an application where data size and SQL queries are going to be heavy. I am thinking between Cassandra or Amazon SimpleDB. Can you please suggest which is more suitable in this kind of ...

representing a many-to-many relationship in couchDB

Let s say I m writing a log analysis application. The main domain object would be a LogEntry. In addition. users of the application define a LogTopic which describes what log entries they are ...

Riak on Windows

I want to play with Riak http://riak.basho.com/ or a least get it running on a Windows system. I have downloaded the source code and compiled it but that s where I get stuck, how do I start it?

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

热门标签