1

I'm unsure how to use EF Core to insert a new entity that has existing children.

I have a list of Products in the database. I am attempting to create a new order that has a list of existing products.

This is the Product class:

public class Product
{
    public long ID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    [Column(TypeName = "decimal(8, 2)")]
    public decimal Price { get; set; }

    public string Category { get; set; }
}

And this is the Order class

public class Order
{
    public long ID { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public bool Giftwrap { get; set; }

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

When I attempt to insert a new order with existing products:

[HttpPost]
public IActionResult Post([FromBody]  Order model)
{
    context.Add(model);
    context.SaveChanges();
    return Ok(model);
}

I get an error:

SqlException: Cannot insert explicit value for identity column in table 'Products' when IDENTITY_INSERT is set to OFF.

This is the DbContext:

using Microsoft.EntityFrameworkCore;

namespace angularsportsstorenetcore2.Models
{
    public class StoreDbContext : DbContext
    {
        public StoreDbContext(DbContextOptions<StoreDbContext> options)
            : base(options) { }

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

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
  • Does the incoming serialized Product array contains valid IDs? If any of them is 0, this flow will try to insert the new product with id as 0 – MKougiouris Oct 04 '21 at 13:43
  • Instead of `Add` use `Update`, it does what in the non-core version of Entity Framework `AddOrUpdate` does. [This](https://stackoverflow.com/q/62449078/9363973) Q&A might help you solve your problem – MindSwipe Oct 04 '21 at 13:45
  • @MKougioris yes the serialized product array does contain the valid ids of the I will update my question. – Bryan Dellinger Oct 04 '21 at 13:46
  • This doesn't make sens ... there should be 3 Tables Orders, OrderProducts(OrderItems) and Products ... if saving `Order` trying to save to `Products` then you does some wrong configuration of many to many relationship – Selvin Oct 04 '21 at 13:48
  • 1
    New parent cannot have existing children. Most probably the relationship is incorrect - Product is not child of Order (neither Order is child of Product), hence you seem to need many-to-many relationship. – Ivan Stoev Oct 04 '21 at 13:50

1 Answers1

0

Thank you all took @Selvin's advice and created a many to many relationship.

   public class OrderProductJunction
    {
        public long Id { get; set; }
        public long OrderId { get; set; }
        public Order Order { get; set; }
        public long ProductId { get; set; }
        public Product Product { get; set; }
    } 



 public class Order
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public bool Giftwrap { get; set; }
        [NotMapped]
        public IEnumerable<Product> Products { get; set; }
        public IEnumerable<OrderProductJunction> OrderProductJunctions { get; set; }

    }

public class Product { public long ID { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [Column(TypeName = "decimal(8, 2)")]
    public decimal Price { get; set; }

    public string Category { get; set; }

    public IEnumerable<OrderProductJunction> OrderProductJunctions { get; set; }
}

and I changed the controller to.

 public IActionResult Post([FromBody]  Order model)
        {
            context.Add(model);
            context.SaveChanges();
            foreach (var item in model.Products)
            {
                context.Add(new OrderProductJunction { OrderId = model.ID, ProductId = item.ID });
            }
            context.SaveChanges();
            return Ok(model);
        }
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79