1

RavenDBに付属するサンプルデータベースにはAlbumsドキュメントコレクションがあり、それぞれに次のGenreようにドキュメントが埋め込まれています。

{
  ... other stuff...
  "Genre": {
    "Id": "genres/1",
    "Name": "Rock"
  },
  ... other stuff...
}

GenreここでhasIdNamefieldsに注意してください。

Genreしかし、ドキュメントを見ると、次のようなId、、、NameおよびDescriptionフィールドがあります。

{
  "Description": "Rock and Roll is a form of rock music developed in the 1950s and 1960s. Rock music combines many kinds of music from the United States, such as country music, folk music, church music, work songs, blues and jazz.",
  "Name": "Rock"
}

コードをモデル化して、独自のドキュメントを保存するときと、ドキュメントを保存して埋め込むときとで、シリアル化と保存方法が異なるようにするにはどうStore()すればよいですか?SaveChanges()GenreAlbumGenre

4

1 に答える 1

3

このモデルは、ravendbサンプルデータと一致します。

public class Genre
{
    public string Id { get; set; }
    public string Description { get; set; }
    public string Name { get; set; }
}

public class Album
{
    public string Id { get; set; }
    public string AlbumArtUrl { get; set; }
    public GenreRef Genre { get; set; }
    public decimal Price { get; set; }
    public string Title { get; set; }
    public int CountSold { get; set; }
    public ArtistRef Artist { get; set; }

    public class GenreRef
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    public class ArtistRef
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

インクルードの使用例:

var albumId = "albums/1";
var album = session.Include<Album>(x => x.Genre.Id)
                   .Load<Album>(albumId);
var genre = session.Load<Genre>(album.Genre.Id);

インクルードとは、ジャンルをロードするときに、セッションですでに追跡されているため、db呼び出しがないことを意味します。

于 2012-10-20T00:19:45.640 に答える