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,130 | Comments: 45,561

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.


  1. RavenDB Conference 2016–Slides - one day from now

There are posts all the way to Jun 01, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats