title: EF Core HasMany vs OwnsMany
body: <p>For <strong>one to many</strong> relationship, what's the difference between <strong>HasMany</strong> and <strong>OwnsMany</strong>? When should I use one over another?</p>
  
  <p>For example:</p>
  
  <pre><code>public class xxx 
  {
      public virtual IReadOnlyCollection&lt;xxxHistoryEntity&gt; Histories =&gt; _histories;
      private readonly List&lt;xxxHistoryEntity&gt; _histories = new List&lt;xxxHistoryEntity&gt;();
  }
  
  public class xxxHistoryEntity : Entity&lt;string&gt;
  {
      public string State { get; set; }
      public string NodeId { get; set; }
      public string Message { get; set; }
  }
  </code></pre>
  
  <p>The Entity Configuration:</p>
  
  <pre><code>class xxxConfiguration
      : IEntityTypeConfiguration&lt;xxx&gt;
  {
      public void Configure(EntityTypeBuilder&lt;xxx&gt; builder)
      {
          builder.OwnsMany(itm =&gt; itm.Histories, collbuilder =&gt;
          {
              collbuilder.HasForeignKey("xxxid");
          });
      }
  }
  
  class xxxHistoryConfiguration
      : IEntityTypeConfiguration&lt;xxxHistoryEntity&gt;
  {
      public void Configure(EntityTypeBuilder&lt;xxxHistoryEntity&gt; builder)
      {
          builder.ToTable("xxx_histories");
          builder.HasKey(itm =&gt; itm.Id);
          builder.Property(itm =&gt; itm.Id)
               .ValueGeneratedOnAdd();
      }
  }
  </code></pre>
  
  <p>The generated migration is below:</p>
  
  <pre><code>        migrationBuilder.CreateTable(
              name: "xxx_histories",
              columns: table =&gt; new
              {
                  id = table.Column&lt;string&gt;(nullable: false),
                  xxxid = table.Column&lt;string&gt;(nullable: false),                    
                  state = table.Column&lt;string&gt;(nullable: true),
                  nodeid = table.Column&lt;string&gt;(nullable: true),
                  message = table.Column&lt;string&gt;(nullable: true),
                  xmin = table.Column&lt;uint&gt;(type: "xid", nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey("PK_xxx_histories", x =&gt; new { x.id, x.xxxid });
                  table.ForeignKey(
                      name: "fk_xxxhistoryentity_xxx_xxxarid",
                      column: x =&gt; x.xxxid,
                      principalTable: "xxx",
                      principalColumn: "id",
                      onDelete: ReferentialAction.Cascade);
              });
  </code></pre>
  
  <p>if I update the <strong>xxxConfiguration</strong> by replacing the OwnsMany with HasMany, like:</p>
  
  <pre><code>class xxxConfiguration
      : IEntityTypeConfiguration&lt;xxx&gt;
  {
      public void Configure(EntityTypeBuilder&lt;xxx&gt; builder)
      {
          builder.HasMany(itm =&gt; itm.Histories)
              .WithOne()
              .HasForeignKey("xxxid");
      }
  }
  </code></pre>
  
  <p>The generated migration is below:</p>
  
  <pre><code>        migrationBuilder.CreateTable(
              name: "xxx_histories",
              columns: table =&gt; new
              {
                  id = table.Column&lt;string&gt;(nullable: false),
                  xxxid = table.Column&lt;string&gt;(nullable: false),
                  state = table.Column&lt;string&gt;(nullable: true),
                  nodeid = table.Column&lt;string&gt;(nullable: true),
                  message = table.Column&lt;string&gt;(nullable: true),
                  xmin = table.Column&lt;uint&gt;(type: "xid", nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey("PK_xxx_histories", x =&gt; new { x.id, x.xxxid });
                  table.ForeignKey(
                      name: "fk_xxxhistoryentity_xxx_xxxid",
                      column: x =&gt; x.xxxid,
                      principalTable: "xxx",
                      principalColumn: "id",
                      onDelete: ReferentialAction.Cascade);
              });
  </code></pre>
  
  <p>As you can see, the migration generated by both are the same. So what's the point of OwnsMany?</p>