Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a many-to-many relationship in Entity Framework designer thingy

I'm an NHibernate developer trying to give Entity Framework a shot in a hobby project. I'm used to specifying mapping data in code using Fluent NHibernate. Pardoning Microsoft's belief that developers shouldn't be allowed to write code, I'm trying to create my mappings using the Entity Framework's visual designer surface (which you get by opening the .edmx file in Visual Studio).

I have no idea how to set up a many-to-many relationship! I have 'updated the model' from the database, but I get two one-to-many relationships with a new entity corresponding to the junction table (which contains only foreign keys and its own primary key).

So far, all attempts at working this out by clicking on the entities and relationships and such have failed. Can anyone give me a pointer?

like image 904
David Avatar asked May 24 '11 20:05

David


People also ask

How will you create relationship between tables in Entity Framework?

You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.

How do I remove a one to many relationship in Entity Framework?

after manual set the property,single call to "dbContext. As. Remove(someA)" work as expected!

How do you create a foreign key relationship in code first approach?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table. Thanks for reading this article, hope you enjoyed it.


1 Answers

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

like image 189
ladenedge Avatar answered Sep 27 '22 23:09

ladenedge