When using OwnsMany within Entity Framework does table splitting occur?

67 Views Asked by At

I am observing that OwnsOne creates the owned entity inline within the owning table but using OwnsMany results in a separate table. From the official documentation I expected both methods to result in a single table by default.

For instance given

public class Author
{
    public int Id { get; set; }
    public Book Book { get; set; }
    
}

public class Book
{
    public string Title { get; set; }  
}

public class AuthorConfiguration : IEntityTypeConfiguration<Author>
{
    public void Configure(EntityTypeBuilder<Author> builder)
    {
        builder.HasKey(m=>m.Id);
        builder.OwnsOne(m => m.Book, n =>
        {
            n.WithOwner().HasForeignKey("AuthorId");
        });
    }
}

I get a table that looks like this

Author
(
    "Id" integer,
    "Book_Title" text
);

When I use OwnsMany with an author class that has a list of books instead

public class Author
{
    public int Id { get; set; }
    public List<Book> Books { get; set; }
}
public class AuthorConfiguration : IEntityTypeConfiguration<Author>
{
    public void Configure(EntityTypeBuilder<Author> builder)
    {
        builder.HasKey(m=>m.Id);
        builder.OwnsMany(m => m.Books, n =>
        {
            n.WithOwner().HasForeignKey("AuthorId");
        });
    }
}

I get a separate Author and Book table, where the Book table looks like

"Book"
(
    "AuthorId" integer NOT NULL,
    "Id" integer NOT NULL ,
    "Title" text NOT NULL
}

Does anyone know if OwnsMany should produce a separate table.

My database is Postgres incidentially.

0

There are 0 best solutions below