Entity Framework Core
实体框架数据访问技术的轻量型、可扩展、开源、跨平台版本。
50 个问题
我有 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?
在代码中,您希望配置具有多个关系的两个实体(部门和学院):一对一和一对多。如果是这种情况,您可以考虑使用该属性。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.
}
然后,迁移后,结果如下:
更新:
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; }
}
然后,迁移后,下表如下:
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。