在.Net Core/EF Core中设置多个相同类的外键 [英] Setting up multiple Foreign keys of same class in .Net Core / EF Core

查看:770
本文介绍了在.Net Core/EF Core中设置多个相同类的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建.Net Core Web应用程序,以将旧的旧版Access ADP/ADE前端替换为项目管理SQL Server数据库.但是,原始数据库的构建者并未设置大量外键关系来支持在SQL视图中进行所有操作.我希望通过此应用程序进行补救.

I'm building a .Net Core web app to replace an old legacy Access ADP/ADE front end to a project management SQL Server database. However, the builder of the original DB didn't setup a whole lot of Foreign key relationships in favor of doing it all in SQL views. Something I'm hoping to remedy with this app.

我要确保在同一个类类型的外键设置时会束手无策.例如,我有以下课程.

I'm wanting to make sure I have my head wrapped around setting up foreign keys when they are of the same class type. For example, I've got the following classes.

Project.cs

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

Employee.cs

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

我在Project Class末尾的按键会像这样吗?

Would my keys at the end of the Project Class look like this?

public Employee PmEmployee { get; set; }
public Employee CadEmployee { get; set; }
public Employee SalesRepEmployee { get; set; }

在员工类别中具有相应的声明

With corresponding declarations in Employee Class

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

DbContext类中的模型构建器

And the Model Builder in my DbContext class

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; }一样分别处理吗?

I guess my biggest concern/question is how the Employee class will handle multiple relationships with the Project Class. Do I need to handle those separately as well like public ICollection<Project> PmProjects { get; set; }?

推荐答案

是的,您需要有单独的项目集合.

Yes, you need to have separate project collections.

在Employee中,您将:

In Employee, you would have:

public ICollection<Project> PmProjects { get; set; }
public ICollection<Project> CadProjects { get; set; }
public ICollection<Project> SalesProjects { get; set; }

在Project中,您将:

In Project, you would have:

public Employee PmEmployee { get; set; }
public Employee CadEmployee { get; set; }
public Employee SalesRepEmployee { get; set; }

生成器为:

modelBuilder.Entity<Project>(entity =>
{
// Fluent API for column properties
...
entity.HasOne(d => d.PmEmployee)
    .WithMany(p => p.PmProjects)
    .HasForeignKey(d => d.PmEmployeeId)
    .OnDelete(DeleteBehavior.SetNull)
    .HasConstraintName("FK_Project_Employee_PM");

entity.HasOne(d => d.CadEmployee)
    .WithMany(p => p.CadProjects)
    .HasForeignKey(d => d.CadEmployeeId)
    .OnDelete(DeleteBehavior.SetNull)
    .HasConstraintName("FK_Project_Employee_CAD");

 entity.HasOne(d => d.SalesRepEmployee)
    .WithMany(p => p.SalesProjects)
    .HasForeignKey(d => d.SalesRepEmployeeId)
    .OnDelete(DeleteBehavior.SetNull)
    .HasConstraintName("FK_Project_Employee_SALES");
 });

这篇关于在.Net Core/EF Core中设置多个相同类的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆