EF Core 2: Database first with many-to-many (linking table)

asp.net-core ef-core-2.0 ef-database-first entity-framework-core

Question

I have many-to-many relationship between Projects and Models in my database

Relationship diagram

I am using this command to generate the .cs models:

Scaffold-DbContext "my connection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir EFModels -Force -Context EFEntities -Tables Projects, Projects_Models, Models

In EF6, this would create Project.Models and Model.Projects properties, without mapping the Projects_Models table into its own class.

But in this case (EF Core), it created a ProjectsModel class, and Project.ProjectsModels and Model.ProjectsModels properties instead.

How can you make it behave like it was in EF6?

1
1
3/23/2018 12:04:49 AM

Accepted Answer

It is not currently possible to do this in EF Core as that feature has not yet been added. From the docs:

Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.

3
3/23/2018 12:52:41 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow