实体框架 - 多个外键问题

时间:2014-05-23 20:56:47

标签: c# entity-framework ef-code-first

我有一个Project模型,其ProjectLeadPerson外键的一个实例),这很好用。但是现在我还需要添加引用相同People表的Person(项目成员)集合,我无法获得实体框架来生成我的数据库。一旦我尝试添加Fluent API代码来创建链接表ProjectPerson,我就会收到错误 - “指定的模式无效。错误:未加载关系'MyApp.WebApi.Models.Person_Projects',因为“MyApp.WebApi.Models.Person”类型不可用。“我认为这是因为现有的FK关系已经存在于ProjectLead

项目模型:

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>();
    }
}

人物模型:

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>();
    }
}

数据库上下文:

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);
    }
}

我认为也许我需要使用InverseProperty属性,但我不确定在这种情况下应该去哪里?

1 个答案:

答案 0 :(得分:1)

您可以明确定义连接表吗?因此,定义一个ProjectPeople关系并使代码类似于这样......

 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;}
 }

然后你的其他两个班级将会是这样的......

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>();
   }

}

这就是..

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>();
  }
}