title: Owned entities with one-to-many relationships in EF Core 3
body: <p>I'm trying to wrap my head around Owned Entities and relationships between them and other Entities.
  I'm working on a very simple example with Products, Quotes and QuoteItems:</p>
  
  <pre><code>public class Product
      {
          public Product(Guid id, decimal price, string name, DateTime creationDate)
          {
              Id = id;
              Price = price;
              Name = name;
              CreationDate = creationDate;
          }
  
          public Guid Id { get; }
          public decimal Price { get; }
          public string Name { get; }
          public DateTime CreationDate { get; }
      }
  
  public class Quote
      {
          public Quote(Guid id, DateTime creationDate)
          {
              Id = id;
              CreationDate = creationDate;
              QuoteItems = new List&lt;QuoteItem&gt;();
          }
  
          public Guid Id { get; }
          public DateTime CreationDate { get; }
          public ICollection&lt;QuoteItem&gt; QuoteItems { get; }
      }
  
  
   public class QuoteItem
      {
          private QuoteItem() { }
          public QuoteItem(Guid id, Product product, int quantity)
          {
              Id = id;
              Quantity = quantity;
              Product = product;
          }
  
          public Guid Id { get; }
          public Product Product { get; }
          public int Quantity { get; }
      }
  </code></pre>
  
  <p>very easy, nothing fancy. Products and Quotes are to be considered the "aggregate root", while the QuoteItems of course are "value objects".</p>
  
  <p>This is how I'm configuring the db context:</p>
  
  <pre><code>public class CommerceDbContext : DbContext
      {
          public CommerceDbContext(DbContextOptions&lt;CommerceDbContext&gt; options)
              : base(options)
          {
              Database.EnsureCreated();
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.ApplyConfiguration(new QuoteEntityTypeConfiguration()); 
          }
  
          public DbSet&lt;Product&gt; Products { get; set; }
  
          public DbSet&lt;Quote&gt; Quote { get; set; }
      }
  
  internal class QuoteEntityTypeConfiguration : IEntityTypeConfiguration&lt;Quote&gt;
      {
          public void Configure(EntityTypeBuilder&lt;Quote&gt; builder)
          {
              builder.ToTable("Quotes", "dbo");
  
              builder.HasKey(r =&gt; r.Id);
  
              builder.Property(e =&gt; e.CreationDate);
  
              builder.OwnsMany(s =&gt; s.QuoteItems, b =&gt;
              {
                  b.ToTable("QuoteItems", "dbo");
                  b.Property(e =&gt; e.Id);
                  b.Property(e =&gt; e.Quantity);
  
                  b.HasOne(e =&gt; e.Product)
                      .WithMany()
                      .HasForeignKey("ProductId");
              });
          }
      }
  </code></pre>
  
  <p>which generates exactly what I want:</p>
  
  <p><a href="https://i.stack.imgur.com/nz8Oo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nz8Oo.png" alt="enter image description here"></a></p>
  
  <p>notice how the QuoteItems table has a FK on Products. Actually it would be nice to have the QuoteId as just FK and not part of the PK, but I'll probably work on it later.</p>
  
  <p>My problem now is that I can create Products with no problem, but when I try to create a quote and add that product I get this error:</p>
  
  <p><a href="https://i.stack.imgur.com/xz8AQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xz8AQ.png" alt="enter image description here"></a></p>
  
  <p>Basically seems that adding a quote will add the product as well as a new entity on the db context, hence creating the issue. But the product has been created previously and correctly written into the db.</p>
  
  <p>Any idea?</p>