0

Doing a shop project. Upon checkout, I create a list of all added product from cart and put it inside Order.Products(M-M relationship):

List<Product> productList = Cart.Select(item => db.Products.SingleOrDefault(x => x.Id == item)).ToList();                        

                    var addOrder = new Order
                    {
                        Id = Guid.NewGuid(),
                        UserId = userId,                                             
                        OrderDate = DateTime.Now,
                        Products = productList
                    };

                    db.Orders.Add(addOrder);
                    db.SaveChanges();

I debugged my code to make sure the productsList had the right amount. But after I added it, I only had one of each added products were added in my db. For e.g. if I have 3x cars, 4x books and 2x boat, then I just get one of each into M-M table, meaning total 3 products, even though I had total 9 in productList.

This is how I set up my m-m relationship.

create table Product_Order (

    ProductId uniqueidentifier foreign key references [Product](ID) not null,
    OrderId uniqueidentifier foreign key references [Order](Id) not null
)

Btw, is it possible to add additional column into Product_Order so called AmountProduct, and set amount value instead making many rows of same products/order? If possible, how do I set amount value inside var addOrder = new Order{? That way would probably be much more effective.

skylu
  • 1
  • 4
  • remove SingleOrDefault that is asking for just a single row – GreatJobBob Dec 18 '16 at 04:30
  • Just add an `Quantity (INT)` column to the `Product_Order` table –  Dec 18 '16 at 04:38
  • @StephenMuecke Ok done, but how do I set the quantity value inside 'var addOrder..{`? – skylu Dec 18 '16 at 04:41
  • Sorry but your code does not make sense. If you adding 3 x `abc` products to your cart, then why are you adding it 3 times instead of once with a quantity of 3? –  Dec 18 '16 at 04:43
  • @StephenMuecke Exactly you're right about that. That idea just hit me and will save space in db. I added a Quantity column but I don't know how set `Quantity` value inside `var addOrder = new Order{` – skylu Dec 18 '16 at 04:47
  • And I do not know how your adding your data to the cart! And it makes no sense that `productList` is typeof `List` - it needs to be `List` –  Dec 18 '16 at 04:51
  • @StephenMuecke Well, I added my db via EF. So I've no `Product_Order` model, since `Product` got `ICollection Orders`, and vice versa etc. Should I manually create a new class-model? – skylu Dec 18 '16 at 04:57
  • @StephenMuecke I added `Product.Id` into Session of list, so called `Cart`. That's how I read all `Product` into `productList` as you can see in my first code line. – skylu Dec 18 '16 at 05:03
  • Yes, you would need a `Product_Order` model in order to save to your `Product_Order` table. And your `Order` class will contain a property `public virtual ICollection Products { get; set; }` –  Dec 18 '16 at 05:03
  • @StephenMuecke I just created a class Product_Order, should I have `public virtual ICollection Orders { get; set; }` **and..** `public virtual ICollection Products { get; set; }` **and..** `public int Quantity { get; set; }` inside it? – skylu Dec 18 '16 at 05:06
  • No - its a model that describes the quantity of a (one) `Product` for an (one) `Order` (not collections of Products and Orders) so you need `int ProductId` and `virtual Product Product`, and `int OrderId` and `virtual Order Order` (plus `int Quantity`) –  Dec 18 '16 at 05:17
  • Refer [this answer](http://stackoverflow.com/questions/19342908/how-to-create-a-many-to-many-mapping-in-entity-framework) for a typical example of many-many mappings with additional properties –  Dec 18 '16 at 05:19
  • @StephenMuecke Lovely, its almost all set now. Just have to figure how to add quantity per product :) Thanks for the guidance. – skylu Dec 18 '16 at 06:01
  • You view should contain a list of products and against each product you have a textbox for the quantity :) –  Dec 18 '16 at 06:05

0 Answers0