Randeep Singh
Randeep Singh

Reputation: 1018

Adding junction table to EF ends up as association

I have couple of tables and a junction table in between.

enter image description here

AspNetUsers and Tenant tables are already added in my Entity Framework.

But once I add UserTenant table, it ends up as association instead of entity.

Can someone enlighten me why this happens?

Upvotes: 1

Views: 177

Answers (1)

JotaBe
JotaBe

Reputation: 39014

This happens because the only function of this junction table is to create a many to many relation.

When EF finds a table which only have columns which are FKs to other tables, and they compose the primary key, it simply converts it to a many-to-many relation.

If you need to have access to the junction table, you ned to break that rule. The easiest way to do so is adding a new column to the junction table which is not part of an FK, neither the PK. If you do so, the junction table will become an entity in your model. But you will lose the easy way to navigate to relate entities (it's still possible, but a bit harder). See this for more details

You can also read this article, which offers an alternate way of doing this, if you use Code First. If you read it, don't miss the comments. There are several very interesting

Upvotes: 3

Related Questions