简体   繁体   English

如何在EF Core中创建关系?

[英]How do I create relationships in EF Core?

I am building a ASP.NET Core 2.0 application with a Web API backend and an Angular 5 front end. 我正在构建带有Web API后端和Angular 5前端的ASP.NET Core 2.0应用程序。 For my data access layer I am using Entity Framework Core. 对于我的数据访问层,我正在使用Entity Framework Core。

For example, I have two models below that have a relationship. 例如,下面有两个具有关联关系的模型。

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

    public string project_name { get; set; }

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

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

    [ForeignKey("pm_person_id")]
    public virtual Person pm_person { get; set; }

    [ForeignKey("ptl_person_id")]
    public virtual Person ptl_person { get; set; }
}



public class Person 
{
    public Person()
    {
        pm_projects = new HashSet<Project>();
        ptl_projects = new HashSet<Project>();
    }

    [Key]
    public int Id { get; set; }

    public string full_name { get; set; }

    [ForeignKey("pm_person_id")]
    public virtual ICollection<Project> pm_projects { get; set; }

    [ForeignKey("ptl_person_id")]
    public virtual ICollection<Project> ptl_projects { get; set; }
}

However when I try and connect to the database and retrieve these to display, EF Core states that it does not understand the relationship between Project.pm_person_id and Person . 但是,当我尝试连接到数据库并检索它们以显示时,EF Core指出它不理解Project.pm_person_idPerson之间的关系。

I followed the microsoft documentation on EF Core and the ForeignKey attribute is what it recommended, as opposed to defining these in the ModelBuilder. 我遵循EF Core上的Microsoft文档,并建议使用ForeignKey属性,而不是在ModelBuilder中进行定义。 Have I fully established the relationship correctly? 我是否已完全正确建立关系?

in the class Project you must add the using 在Project类中,您必须添加using

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

and the class Project should be modeled in this way: 并且应该以这种方式对Project类进行建模:

[ForeignKey("pm_person_id")]
public int pm_person_id { get; set; }
public Person Person { get; set; }

and add the icollection in the class Person 并将icollection添加到Person类中

public ICollection<Project> Project { get; set; }

try this! 尝试这个!

For each foreign key put an id property and a property with the datatype of the foreign key class and you don't need any annotations entity framework will understand that it is a foreign key 为每个外键放置一个id属性和一个具有外键类数据类型的属性,并且您不需要任何注释,实体框架将理解它是一个外键

public class Project 
{
  public int ID { get; set; }

  public string projectName { get; set; }

  public int? pmPersonID { get; set; }
  public Person pmPerson { get; set; }

  public int? ptlPersonID { get; set; }
  public Person ptlPerson { get; set; }
}



public class Person 
{
    public Person()
  {
    pm_projects = new HashSet<Project>();
    ptl_projects = new HashSet<Project>();
  }

  public int ID { get; set; }

  public string fullName { get; set; }

  public int pmPersonId { get; set; }
  public virtual ICollection<Project> pmProjects { get; set; }

  public int ptlProjectsId { get; set; }
  public virtual ICollection<Project> ptlProjects { get; set; }

} }

Not sure exactly what you are looking for here, but is a more in-depth example which may help. 不确定确切要在这里找到什么,但是这是一个更深入的示例,可能会有所帮助。

public class Person
{
    public Person()
    {
        PM_Projects = new HashSet<Project>();
        PTL_Projects = new HashSet<Project>();
    }

    [Key]
    [Required]
    public int Id { get; set; }

    public string Full_Name { get; set; }

    public virtual ICollection<Project> PM_Projects { get; set; }

    public virtual ICollection<Project> PTL_Projects { get; set; }
}

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

    public string Project_Name { get; set; }

    [ForeignKey("PM_Person")]
    public int PM_Person_Id { get; set; }

    [ForeignKey("PTL_Person")]
    public int PTL_Person_Id { get; set; }

    public virtual Person PM_Person { get; set; }

    public virtual Person PTL_Person { get; set; }
}

The classes for some mapping 一些映射的类

public class PersonMap : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {

        builder.Property(t => t.Full_Name);

        //table  
        builder.ToTable("People");

        //relationships
        builder.HasMany(s => s.PM_Projects).WithOne(o=>o.PM_Person).OnDelete(DeleteBehavior.Restrict);
        builder.HasMany(s => s.PTL_Projects).WithOne(o => o.PTL_Person).OnDelete(DeleteBehavior.Restrict);
    }
}

public class ProjectMap : IEntityTypeConfiguration<Project>
{
    public void Configure(EntityTypeBuilder<Project> builder)
    {

        builder.Property(t => t.Project_Name);

        //table  
        builder.ToTable("Projects");

        //relationships
        builder.HasOne(s => s.PM_Person).WithMany(d=>d.PM_Projects).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(s => s.PTL_Person).WithMany(d => d.PTL_Projects).OnDelete(DeleteBehavior.Restrict);

    }
}

On model creating 关于模型创建

protected override void OnModelCreating(ModelBuilder builder)
{
        base.OnModelCreating(builder);


        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        //Build identity
        BuildIdentity(builder);

        builder.ApplyConfiguration(new PersonMap());
        builder.ApplyConfiguration(new ProjectMap());
}

Add and run your migrations 添加并运行您的迁移

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

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