Inserting data in many to many relation ship in EF core

Erum Mirza 6 Reputation points
2023-06-05T15:28:34.3533333+00:00

i have two tables

1-Employees (ID ,EmpName)

2- Project (ID,ProjectName)

data is already inserted in boh tables ,there is many to many relationship between these two so middle table created in table i.e EmployeesProject

now i want to insert data (keys of both table i.e EmpID,ProjectID ) in Midddle in order to maintain Many to many relationship,can some one help me how to do this .. please not down i m using EFcore .

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
751 questions
{count} vote

1 answer

Sort by: Most helpful
  1. AgaveJoe 28,536 Reputation points
    2023-06-05T16:39:46.0433333+00:00

    You did not share the entity configuration or tell us what kind of application you're building. In a typical application flow one of the entities is selected in the user interface (UI). For example, the Project is selected in the UI. The UI shows all the employees in the project with an option to add an employee to the selected project. This assumes the Project entity has collection of Employees property. From there it's a simply matter of adding the an employee to the collection of employees and saving the entity.

    I think you'll be interested in reading the EF many-to-many relationship documentation.

    Many-to-many relationships

    Also, the official ASP.NET Core data tutorials have a many-to-many relationship example with common programming patterns.

    Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8

    ASP.NET Core MVC with EF Core - tutorial series

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.