I am using EF to insert and retrieve objects from Cosmos DB. However my object and its nested child object are being saved into cosmos as separate documents rather than both being in the same one.
I have the following Objects
public class Author
{
public Guid AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public List<Book> Books { get; set; }
}
public class Book
{
public Guid BookId { get; set; }
public string Title { get; set; }
public Author Author { get; set; }
}
And the following DbContext
public class BookStoreDbContext:DbContext
{
private static readonly string EndpointUri = ConfigurationManager.AppSettings["EndPointUri"];
private static readonly string PrimaryKey = ConfigurationManager.AppSettings["PrimaryKey"];
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCosmos(
EndpointUri,
PrimaryKey,
databaseName: "BookStoreDb");
}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
}
This is the code used to insert the data
using (var context = new BookStoreDbContext())
{
context.Database.EnsureCreated();
var authors = new List<Author>
{
new Author
{
AuthorId = Guid.NewGuid(),
FirstName ="Meredith",
LastName ="Alonso",
BirthDate = DateTime.Parse("1970-09-01"),
Books = new List<Book>()
{
new Book { BookId=Guid.NewGuid(), Title = "Introduction to Microeconomics"}
}
}
};
context.Authors.AddRange(authors);
context.SaveChanges();
}
I then end up with an Author Object and a Book object saved into the db rather than an Author Object containing a nested Book Object...
What am I doing wrong?