我正在构建一个 .Net Core Web 应用程序,以替换项目管理 SQL Server 数据库的旧版 Access ADP/ADE 前端。然而,原始数据库的构建者并没有设置大量外键关系,而是在 SQL 视图中完成这一切。我希望通过这个应用程序来解决一些问题。
当外键属于同一类类型时,我想确保我全神贯注于设置外键。例如,我有以下课程。
项目.cs
using System;
using System.Collections.Generic;
namespace ProjectLogic.Models
{
public class Project
{
public int Id { get; set; };
public string Name { get; set; }
public int? PmEmployeeId { get; set; }
public int? CadEmployeeId { get; set; }
public int? SalesRepEmployeeId { get; set; }
...
}
}
员工.cs
using System;
using System.Collections.Generic;
namespace ProjectLogic.Models
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
...
}
}
项目课程结束时我的钥匙会像这样吗?
public Employee PmEmployee { get; set; }
public Employee CadEmployee { get; set; }
public Employee SalesRepEmployee { get; set; }
在 Employee 类中有相应的声明
public ICollection<Project> Projects { get; set; }
以及我的 DbContext 类中的模型生成器
modelBuilder.Entity<Project>(entity =>
{
// Fluent API for column properties
...
entity.HasOne(d => d.PmEmployee)
.WithMany(p => p.Projects)
.HasForeignKey(d => d.PmEmployeeId)
.OnDelete(DeleteBehavior.SetNull)
.HasConstraintName("FK_Project_Employee_PM");
entity.HasOne(d => d.CadEmployee)
.WithMany(p => p.Projects)
.HasForeignKey(d => d.CadEmployeeId)
.OnDelete(DeleteBehavior.SetNull)
.HasConstraintName("FK_Project_Employee_CAD");
entity.HasOne(d => d.SalesRepEmployee)
.WithMany(p => p.Projects)
.HasForeignKey(d => d.SalesRepEmployeeId)
.OnDelete(DeleteBehavior.SetNull)
.HasConstraintName("FK_Project_Employee_SALES");
});
我想我最大的担忧/问题是 Employee 类将如何处理与 Project 类的多种关系。我是否也需要单独处理这些public ICollection<Project> PmProjects { get; set; }
?