1

RavenDB 附带的示例数据库具有Albums文档集合,每个集合中都有一个Genre嵌入文档,如下所示:

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

请注意,这里有GenrehasIdNamefields。

但是当您查看Genre文档时,它们具有IdNameDescription字段,如下所示:

{
  "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 回答