Holland Risley
Holland Risley

Reputation: 6781

Entity Framework - Multiple Foreign Keys Issue

I have a Project model which has a ProjectLead (one instance of the Person Foreign Key), this works fine. But now I also need to add a collection of People (Project members) referencing the same Person table and I can't get the Entity Framework to generate my database. As soon as I try to add the Fluent API code to create the link table ProjectPerson I get an error - "Schema specified is not valid. Errors: The relationship 'MyApp.WebApi.Models.Person_Projects' was not loaded because the type 'MyApp.WebApi.Models.Person' is not available." I assume this is because of the existing FK relationship already in place with ProjectLead.

Project Model:

public class Project
{
    [Key]
    public int ProjectId { get; set; }

    public string Name { get; set; }

    // Foreign Key - Project lead (Person)
    public int ProjectLeadId { get; set; }
    public virtual Person ProjectLead { get; set; }

    // Create many to many relationship with People - Team members on this project
    public ICollection<Person> People { get; set; }

    public Project()
    {
        People = new HashSet<Person>();
    }
}

Person Model:

public class Person
{
    [Key]
    public int PersonId { get; set; }

    public String Firstname { get; set; }
    public String Surname { get; set; }

    // Create many to many relationship
    public ICollection<Project> Projects { get; set; }

    public Person()
    {
        Projects = new HashSet<Project>();
    }
}

DB Context:

public class HerculesWebApiContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Project> Projects { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // This works fine
        modelBuilder.Entity<Project>()
                    .HasRequired(c => c.ProjectLead)
                    .WithMany(d => d.Projects)
                    .HasForeignKey(c => c.ProjectLeadId)
                    .WillCascadeOnDelete(false);

        // Adding these lines to create the link table `PersonProjects` causes an error

        //modelBuilder.Entity<Person>().HasMany(t => t.Projects).WithMany(t => t.People);

        //modelBuilder.Entity<Project>().HasMany(t => t.People).WithMany(t => t.Projects);
    }
}

I gather that perhaps I need to use the InverseProperty attribute, but I am not sure where this should go in this case?

Upvotes: 0

Views: 674

Answers (1)

Robert Chumley
Robert Chumley

Reputation: 119

Can you explicitly define your join table? So, define a ProjectPeople relationship and make the code something like this...

 public class ProjectPerson{
    [Key]
    public int ProjectPersonId { get; set; }

    [ForeignKey("Project")]
    public int? ProjectId {get;set;}
    public virtual Project {get;set;}

    [ForeignKey("Person")]
    public int? PersonId {get;set;}
    public virtual Person {get;set;}

    public string RelationshipType {get;set;}
 }

Then your other 2 classes will look like this...

public class Project
{
   [Key]
   public int ProjectId { get; set; }

   public string Name { get; set; }

   // Foreign Key - Project lead (Person)
   public int ProjectLeadId { get; set; }
   public virtual Person ProjectLead { get; set; }

   // Create many to many relationship with People - Team members on this project
   public virtual ICollection<ProjectPerson> ProjectPeople { get; set; }

   public Project()
   {
       ProjectPerson = new HashSet<ProjectPerson>();
   }

}

And this..

Public class Person
{
  [Key]
  public int PersonId { get; set; }

  public String Firstname { get; set; }
  public String Surname { get; set; }

  // Create many to many relationship
  public virtual ICollection<ProjectPerson> ProjectPeople { get; set; }


  public Person()
  {
     ProjectPerson = new HashSet<ProjectPerson>();
  }
}

Upvotes: 1

Related Questions