I have a table called Products which obviously contains products.
However, I need to create related products. So what I’ve done is create a junction table called product_related which has two PKs. ProductID from Products table and RelatedID also from Products table.
I already use EF and have set up everything on other tables. How should I add this properly in order to create a relationship with products as such:
product.Products.Add(product object here). Of course here product represent a product object that I’ve fetched from the db using db.Products.FirstOr....
How should I do this properly ? A many to many to the same table?
Thanks.
In order to create a many-to-many relationship with Database-First approach you need to setup a database schema that follows certain rules:
Productstable with a columnProductIDas primary keyProductRelationstable with a columnProductIDand a columnRelatedIDand mark both columns as primary key (composite key)ProductRelationstable. The two key columns must be the only columns in the table to let EF recognize this table as a link table for a many-to-many relationshipProductstable as primary-key-table with theProductIDas primary key and theProductRelationstable as foreign-key-table with only theProductIDas foreign keyProductstable as primary-key-table with theProductIDas primary key and theProductRelationstable as foreign-key-table with only theRelatedIDas foreign keyIf you generate an entity data model from those two tables now you will get only one entity, namely a
Productentity (or maybeProductsif you disable singularization). The link tableProductRelationswon’t be exposed as an entity.The
Productentity will have two navigation properties:These navigation collections are the two endpoints of the same many-to-many relationship. (If you had two different tables you wanted to link by a many-to-many relationship, say table
AandB, one navigation collection (Bs) would be in entityAand the other (As) would be in entityB. But because your relationship is “self-referencing” both navigation properties are in entityProduct.)The meaning of the two properties are:
Productsare the products related to the given product,Products1are the products that refer to the given product. For example: If the relationship means that a product needs other products as parts to be manufactured and you have the products “Notebook”, “Processor”, “Silicon chips” then the “Processor” is made of “Silicon chips” (“Silicon chips” is an element in theProductscollection of theProcessorproduct entity) and is used by a “Notebook” (“Notebook” is an element in theProducts1collection of theProcessorproduct entity). Instead ofProductsandProducts1the namesMadeOfandUsedBywould be more appropriate then.You can safely delete one of the collections from the generated model if you are only interested in one side of the relationship. Just delete for example
Products1in the model designer surface. You can also rename the properties. The relationship will still be many-to-many.Edit
As asked in a comment the model and mapping with a Code-First approach would be:
Model:
Mapping: