当 Principle 实体中的两个属性指向依赖实体中的同一属性时,如何使用 EF Core?

Hui Liu-MSFT 40,666 信誉分 Microsoft 供应商
2024-04-16T07:26:43+00:00

我有 2 个模型类:


Department.cs

public class Department
{
    public string DepartmentId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [ForeignKey("FacultyId")]
    public IList<Faculty> Faculties{ get; set; }

    [ForeignKey("FacultyId")]
    public Faculty HOD { get; set; }

}


Faculty.cs


public class Faculty
{
    public string FacultyId { get; set; }

    public string FacultyName { get; set; }

    public FacultyStatus FacultyStatus { get; set; }

    public Designation Designation { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string PhoneNumber { get; set; }

    public string Address { get; set; }

    public int Experience { get; set; }

    public string Qualifications { get; set; }

    public string Description { get; set; }

    public Department Department{ get; set; }

}

我无法确定如何为部门设置 HOD。我想到的是,将 HOD 列作为外键,但我不知道这会有多好或有用。 我参考了这个链接来学习,但我无法应用相同的链接


错误:

无法确定类型为“ICollection<Faculty>”的导航“Department.Faculties”所表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。

Note:此问题总结整理于: How to use EF Core when two properties in the Principle entity point to the same attribute in the dependent entity?

Entity Framework Core
Entity Framework Core
实体框架数据访问技术的轻量型、可扩展、开源、跨平台版本。
38 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 34,201 信誉分 Microsoft 供应商
    2024-04-16T09:37:15.16+00:00

    在代码中,您希望配置具有多个关系的两个实体(部门和学院):一对一和一对多。如果是这种情况,您可以考虑使用该属性。InverseProperty

    例如:

    public class Department  
    {  
        public string DepartmentId { get; set; }  
        public string Name { get; set; }  
        public string Description { get; set; }  
    
        [InverseProperty("FacultyDepartment")]  
        public IList<Faculty> Faculties { get; set; } //navigation property, One-to-Many relationship  
    
        [InverseProperty("HODDepartment")]  
        public Faculty HOD { get; set; }        //navigation property, One-to-One relationship  
    }  
    public class Faculty  
    {  
        [ForeignKey("HODDepartment")]  
        public string FacultyId { get; set; }   // this property is the primary key, and the one-to-one relationship foreign key. You can also add a new Primary key.  
    
        public string FacultyName { get; set; }  
           
           
        [DataType(DataType.EmailAddress)]  
        public string Email { get; set; }  
        [DataType(DataType.PhoneNumber)]  
        public string PhoneNumber { get; set; }  
        public string Address { get; set; }  
    
        public int Experience { get; set; }  
    
        public string Qualifications { get; set; }  
    
        public string Description { get; set; }  
    
        public Department HODDepartment { get; set; }  // one-to-one relationship, navigation property.  
    
        [ForeignKey("FacultyDepartment")]  
        public string FacultyDepartmentId { get; set; }  //one-to-many relationship foreign key.  
        public Department FacultyDepartment { get; set; } //one to-many relationship , navigation property.  
    }  
    

    然后,迁移后,结果如下:

    145587-image.png

    更新:

    HOD(这应该是作为 HOD 的学院的 ID)

    若要在 DepartMent 表中添加 HodId,可以基于前面的示例代码,在 Departments 类中添加外键:

    public class Department  
    {  
        public string DepartmentId { get; set; }  
        public string Name { get; set; }  
        public string Description { get; set; }  
        [InverseProperty("FacultyDepartment")]  
        public IList<Faculty> Faculties { get; set; }  
      
        [ForeignKey("HOD")]  
        public string HODId { get; set; }  
        public Faculty HOD { get; set; }  
    }  
    

    学院班级如下:

    public class Faculty  
    {  
        [ForeignKey("HODDepartment")]  
        public string FacultyId { get; set; }  
    
        public string FacultyName { get; set; }  
           
           
        [DataType(DataType.EmailAddress)]  
        public string Email { get; set; }  
        [DataType(DataType.PhoneNumber)]  
        public string PhoneNumber { get; set; }  
        public string Address { get; set; }  
    
        public int Experience { get; set; }  
    
        public string Qualifications { get; set; }  
    
        public string Description { get; set; }  
        public Department HODDepartment { get; set; }  
    
        [ForeignKey("FacultyDepartment")]  
        public string FacultyDepartmentId { get; set; }  
        public Department FacultyDepartment { get; set; }  
    }  
    

    然后,迁移后,下表如下:

    146115-image.png


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助