如何在EF Core中创建关系?

时间:2018-04-19 19:16:14

标签: c# asp.net-core entity-framework-4.1

我正在构建一个带有Web API后端和Angular 5前端的ASP.NET Core 2.0应用程序。对于我的数据访问层,我正在使用Entity Framework Core。

例如,我有两个有关系的模型。

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

但是,当我尝试连接到数据库并检索要显示的数据时,EF Core声明它不理解Project.pm_person_idPerson之间的关系。

我遵循了EF Core上的microsoft文档,并且推荐使用ForeignKey属性,而不是在模型构建器中定义它们。我是否正确地建立了这种关系?

3 个答案:

答案 0 :(得分:0)

在Project类中,您必须添加使用

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

并且应该以这种方式建模类Project:

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

并在类Person

中添加icollection
public ICollection<Project> Project { get; set; }

试试这个!

答案 1 :(得分:0)

对于每个外键,将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; }

}

答案 2 :(得分:0)

不确定你到底在找什么,但这是一个更深入的例子,可能有所帮助。

<?php

$map_city = ['2'=>'Madina','3'=>'Makkah'];
$single_group = ['1'=>'Single','2'=>'Group'];

$data = [
    [
        'city_form' => 2,
        'city_to' => 3,
        'single_or_group_id' => 1,
        'rent' => 1000
    ],

    [
        'city_form' => 2,
        'city_to' => 3,
        'single_or_group_id' => 2,
        'rent' => 4000
    ],

];

?>

<table style="width:25%" border=1>

    <?php $is_first = true; ?>
    <?php foreach($data as $single){ ?>

    <?php if($is_first) { ?>
      <tr>
          <th colspan=2><?php echo $map_city[$single['city_form']] .'-'. $map_city[$single['city_to']];?></th>
      </tr>

      <tr>
         <?php foreach($data as $single){ ?>
             <td><?php echo $single_group[$single['single_or_group_id']];?></td>
         <?php } ?>
      </tr>

      <tr>
         <?php foreach($data as $single){ ?>
             <td><?php echo 'S.R '.$single['rent'];?></td>
         <?php } ?>
      </tr>

        <?php $is_first = false; } ?>

    <?php } ?>

</table>

某些映射的类

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

关于模型创建

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

    }
}

添加并运行迁移