简体   繁体   中英

Owned entities with one-to-many relationships in EF Core 3

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:

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<QuoteItem>();
        }

        public Guid Id { get; }
        public DateTime CreationDate { get; }
        public ICollection<QuoteItem> 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; }
    }

very easy, nothing fancy. Products and Quotes are to be considered the "aggregate root", while the QuoteItems of course are "value objects".

This is how I'm configuring the db context:

public class CommerceDbContext : DbContext
    {
        public CommerceDbContext(DbContextOptions<CommerceDbContext> options)
            : base(options)
        {
            Database.EnsureCreated();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new QuoteEntityTypeConfiguration()); 
        }

        public DbSet<Product> Products { get; set; }

        public DbSet<Quote> Quote { get; set; }
    }

internal class QuoteEntityTypeConfiguration : IEntityTypeConfiguration<Quote>
    {
        public void Configure(EntityTypeBuilder<Quote> builder)
        {
            builder.ToTable("Quotes", "dbo");

            builder.HasKey(r => r.Id);

            builder.Property(e => e.CreationDate);

            builder.OwnsMany(s => s.QuoteItems, b =>
            {
                b.ToTable("QuoteItems", "dbo");
                b.Property(e => e.Id);
                b.Property(e => e.Quantity);

                b.HasOne(e => e.Product)
                    .WithMany()
                    .HasForeignKey("ProductId");
            });
        }
    }

which generates exactly what I want:

在此处输入图像描述

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.

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:

在此处输入图像描述

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.

Any idea?

solved by loading the Product instance from the same DbContext used to save the Quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM