C
C#3w ago
B.r.a.iS

EF Core - Intermediate table or Navigation Properties?

Hi everyone, A beginner question here. I’m working on a .NET project using Entity Framework Core and SQL Server. I have two entities, Organizations and Modules, where an organization can have multiple modules, and a module can belong to multiple organizations. I initially thought of creating an intermediate table OrganizationModules to handle this many-to-many relationship. My question is: With Entity Framework Core, do I still need to explicitly define this intermediate table, or can I just rely on navigation properties (ICollection<Module> in Organization and ICollection<Organization> in Module) and let EF Core handle the join table behind the scenes? Also, when would you recommend defining the intermediate table as an entity explicitly? Thank you!
2 Replies
Jimmacle
Jimmacle3w ago
simple join tables will be discovered by convention, you shouldn't have to explicitly configure them just having the 2 collection navigations should be enough i'd make it an explicit entity if you actually have to store extra data with that relationship, like the table isn't just Entity1Id, Entity2Id
B.r.a.iS
B.r.a.iS3w ago
In this case I would only store the ids of both tables, in principle it would not be necessary to store any additional data. So I will proceed to code with the 2 navigation collections in that case Thank you!!
Want results from more Discord servers?
Add your server