question

DIEGOFELIPEROMEROVALDES-5270 avatar image
0 Votes"
DIEGOFELIPEROMEROVALDES-5270 asked AgaveJoe commented

Concatenate multiple rows into a single one in the same table with Razor and Create operation

Hello everyone

I'm building an inventory system where i have the Products table with:

PK ID - Name - Category

I need to assign Products to existing products in this table. Maybe this sounds a little bit confusing so i designed an example:

145755-captura.png


This is the table i currently have

145853-captura2.png

This is what i want to achieve but i don't know how.

My questions are, how can i edit my Products model for a correct reference of these items? I should use varchar, or a list, or reference items by it's id?

If it's via String, how can i concatenate the multiple rows into that one?

dotnet-aspnet-mvcdotnet-entity-framework
captura.png (3.0 KiB)
captura2.png (4.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe commented

You'll need to fix the design otherwise you'll be pulling your hair out when it comes time to query the data. Assignable items should exist in a separate table maybe named Accessory including a primary key that identifies a Mouse (1) and a Keyboard (2). Create a third table that relates an Accessory to a Product named ProductAccessories. The ProductAccessories table will have a ProductId and an AccessoryId column which makeup the primary key for the ProductAccessories table.

The Laptop in your example will have two records in the ProductAccessories table.

1 (laptop) | 1 (mouse)
1 (laptop) | 2 (keyboard)


By the way, this is called a many-to-many relationship and explained in another one of your threads on this subject. Please see the Entity Framework docs to learn navigation basics.

Relationships, navigation properties, and foreign keys
Code First Data Annotations
Fluent API - Relationships


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey Joe, thank you very much again. I designed the model again and i want to know if it's possible that a table exists without have a Primary Key. I did a research on google and people say yes, but i got a warning in Visual Studio: "Error 6002: The table/view does not have a primary key defined"

Anyway, i will post you my db diagram:

146251-captura.png

Thank you so much in advance


0 Votes 0 ·
captura.png (10.5 KiB)
AgaveJoe avatar image AgaveJoe DIEGOFELIPEROMEROVALDES-5270 ·

As explained above (and your other thread) The two IDs, ProductID and AccessoryID, make up a composite primary key. Use the Fluent API to define the primary key for the ProductAccessories table. The Fluent API - Relationships link in my first post cover how to configure a composite key.


0 Votes 0 ·