在.Net Core / EF Core

时间:2017-07-26 19:17:01

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

我正在构建一个.Net Core Web应用程序,将旧的旧版Access ADP / ADE前端替换为项目管理SQL Server数据库。但是,原始数据库的构建器没有设置大量的外键关系,而是支持在SQL视图中完成所有操作。我希望用这个应用程序来补救的东西。

我想确保当他们属于同一类型时,我的脑袋就会设置外键。例如,我有以下课程。

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

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末尾的键是否会像这样?

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

在Employee Class中使用相应的声明

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 Class的多个关系。我是否需要单独处理这些内容,例如public ICollection<Project> PmProjects { get; set; }

1 个答案:

答案 0 :(得分:2)

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

在员工中,您将拥有:

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

在Project中,您将拥有:

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