Home > Enterprise >  Many Relations Between 2 tables - Code First
Many Relations Between 2 tables - Code First

Time:09-17

I am building a relationship between 2 tables (code first [entity framework]). But I have the following issue:

Actual Relation Between Employees-Projects

The table Employees saves the employees with different positions (engineers, technicians, salesmen, etc).

In the table project, I have 3 relations with the table employees:

  1. Manager = Employee with the engineer position
  2. Technician = Employee with the technician position
  3. Saleman (EmployeeId) = Employee with the saleman position

Is it possible to create more than one relationship between two tables with EntityFrameWork or do I need to do this (i'm not sure about this).

New Relation Between Employees-Projects

However, I going to create a table for the positions related with Employees.

CodePudding user response:

If you have 3 Fk in your project table... You can set 3 Fk with fluent api...

modelBuilder.Entity<product>()
            .HasOne<employees>(s => s.Eng)
            .WithMany(g => g.progectEng)
            .HasForeignKey(s => s.ProjectEngFK);

modelBuilder.Entity<product>()
            .HasOne<employees>(s => s.Tech)
            .WithMany(g => g.progectTech)
            .HasForeignKey(s => s.ProjectTechFK);
  • Related