简体   繁体   English

实体框架-多个外键问题

[英]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. 我有一个具有ProjectLeadPerson外键的一个实例)的Project模型,这很好用。 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. 但是现在我还需要添加一个引用同一Person表的People (项目成员)集合,而我无法获得实体框架来生成数据库。 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." 当我尝试添加Fluent API代码以创建链接表ProjectPerson ,出现错误-“指定的架构无效。错误:由于类型为'MyApp,未加载关系'MyApp.WebApi.Models.Person_Projects' .WebApi.Models.Person'不可用。” I assume this is because of the existing FK relationship already in place with ProjectLead . 我认为这是因为ProjectLead已存在现有的FK关系。

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? 我收集到可能需要使用InverseProperty属性,但是我不确定在这种情况下应该在哪里使用?

Can you explicitly define your join table? 您可以显式定义联接表吗? So, define a ProjectPeople relationship and make the code something like this... 因此,定义一个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;}
 }

Then your other 2 classes will look like this... 然后您的其他2个类将如下所示...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM