Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,007 | Comments: 44,760

filter by tags archive

Porting MVC Music Store to RavenData migration

time to read 3 min | 495 words

Here is the code required to take the data in the MVC Music Store database and turn into the appropriate Raven documents:

using (var documentStore = new DocumentStore { Url = "http://localhost:8080" })
    using (var session = documentStore.OpenSession())
        foreach (var album in storeDB.Albums.Include("Artist").Include("Genre"))
                Id = "albums/" + album.AlbumId,
                Arist = new { album.Artist.Name, Id = "artists/" + album.Artist.ArtistId },
                Genre = new { album.Genre.Name, Id = "genres/" + album.Genre.GenreId },
        foreach (var genre in storeDB.Genres)
                Id = "genres/" + genre.GenreId

As you can see, it is pretty simple and, even if I say so myself, pretty slick.

I use anonymous types here because I am only concerned with porting the data, I don’t really care about how to deal types right now.

More posts in "Porting MVC Music Store to Raven" series:

  1. (31 May 2010) StoreManagerController, part 2
  2. (29 May 2010) StoreManagerController
  3. (28 May 2010) Porting the checkout process
  4. (25 May 2010) StoreController
  5. (24 May 2010) Advanced Migrations
  6. (23 May 2010) Migrations
  7. (22 May 2010) Porting the HomeController, the Right Way
  8. (21 May 2010) Porting the HomeController, the map/reduce way
  9. (20 May 2010) Data migration
  10. (19 May 2010) Setting up the application
  11. (18 May 2010) The data model


Bruce Boughton

Are those object initializer well-formed? Doesn't look like that.

Ryan Heath

Looks good to me, for instance,





Id = "genres/" + genre.GenreId


will create an anonymous type with 3 properties: Description, Name and Id.

// Ryan


I am really interested to see how you can solve the problem that denormalized data must be updated in many places. I am sure your very interesting series will cover that.


I assume that the string literals are a consequence of using anonymous types. Also, what's the story around the Id generation? I assume there isn't an equivalent to autoincrement Ids built into Raven.

Demis Bellot

Hi Ayende,

It's good that you have a convenient API for data access, looks very nice and succinct - which I think is very important for code maintenance and readability.

Although personally I've never liked anonymous types and have always preferred to have a strongly-typed schema to bind to as it makes re-factoring a little safer.

I haven't tried it but does R# support re-factoring anon types by default? since it does comments and literals (and jetbrains are awesome) it wouldn't surprise me.

Ayende Rahien


Oh, I am going to use strongly typed stuff for everything, but this is just the first stage, getting stuff into Raven.

We will see the entity schema shortly

And yes, R# support anon type to named type


I downloaded RavenDB the other night and thought it was mint. This is more to do with my lack of understanding of document storage design than the above code so forgive me for going off topic slightly. What happens when you store a million albums each of which have the category "Reggae" and once day you decide to re-name the category to "Reggae & Dub-Step"? I take it you'd just update those million records with the new category name and then save them back to the object database? (ie with a million writes)

In a traditional relational database model you'd change Genre name field in Genre Table and all Album records that point to this record pick up that change indirectly, so a single write to update a million records?

Ayende Rahien

thought it was mint.

I am not familiar with that term, is this good or bad?

Raven does batch writes, so it would be a single write to a million docs.

But more to the point, you have to understand that there are different tradeoffs with how you design a system.

You can certainly reference the genre id and then get the name from a master doc if you like, but it would mean putting more work on the read.

In most systems, reads are far more often than writes, it makes sense to do it this way.

In addition to that, you tend to denormalize things that won't change frequently. Tell me how often a Genre name change, for example


It's a good thing :)

Mint is a term used to describe something of high quality. Like if you restored an old car to "mint" condition.

Alok Gupta

Its a good example but I have some problems when I am creating the database .

Plaese can comeone help me?

Thanks in Advance

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Speaking (3):
    23 Sep 2015 - Build Stuff 2015 (Lithuania & Ukraine), Nov 18 - 24
  2. Production postmortem (11):
    22 Sep 2015 - The case of the Unicode Poo
  3. Technical observations from my wife (2):
    15 Sep 2015 - Disk speeds
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats