How to set up many-to-many relationships in Entity Framework thingy design

I am an NHibernate developer trying to give the Entity Framework a chance for success in a hobby. I use to determine display data in code using Fluent NHibernate. Forgive Microsoft that developers are not allowed to write code, I'm trying to create my mappings using the Entity Framework visual designer surface (which you get by opening the .edmx file in Visual Studio).

I don’t know how to set up many-to-many relationships! I “updated the model” from the database, but I get two one-to-many relationships with the new entity corresponding to the transition table (which contains only foreign keys and its own primary key).

So far, all attempts to figure this out by clicking on entities and relationships, etc., have failed. Can someone give me a pointer?

+6
source share
2 answers

Your join table should be what MS calls the ' clean join table ' - it should contain only two foreign keys, and no other columns. In your case, this means that you must delete the primary key column.

+9
source

When you add an association to the model, you select each of the two tables and select “many” on either side of the relationship. When this generates a script database, a connection table with two keys will be created for you.

enter image description here

By the way, if you do not like to use Model-first and will rather conduct code-First code research for the Entity Framework. You can also do Database-First if you want.
+2
source

Source: https://habr.com/ru/post/888953/


All Articles