0

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?

3 Answers 3

1

If you want the objects to be nested, you need to use owned entities

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    modelBuilder.Entity<Author>().OwnsOne(a => a.Book);
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you remove the DBSet defined for Book, then EF will nest that entity within Author. I believe in EF 6 on you won't need to explicitly define ownership, it will imply it, but having the DBSet defined for Book is telling it to store those in their own container.

Comments

0

I have realised that my understanding of how EF Works with Cosmos was lacking...

EF splits the objects out and saves them as separate documents despite the fact that they are child objects of the parent.

So the fact that the objects are split out is not a bug it is simply how EF handles cosmos.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.