使用实体框架从Cosmos Db获取子文档



我有一个Author对象,它有一个图书列表作为子对象,如下所示

public class Author
{
public Guid AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<Book> Books { get; set; } = new HashSet<Book>();
}

这将作为两个单独的文档存储在CosmosDb中(因为这似乎是EF写入Cosmos的方式)。

但是如果我做下面的

using (var context = new BookStoreDbContext())
{
var list = context.Authors;
foreach (var author in list)
{
Console.WriteLine(author.FirstName + " " + author.LastName);
foreach (var book in author.Books)
{
Console.WriteLine("t" + book.Title);
}
}
}

The Books collection is empty…

我怎么让它还书?

我发现了这个问题。我需要在BookStoreDbContext

中添加以下代码
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().OwnsMany(t => t.AccountUsers);
}

最新更新