table 怎么会有两个可为空的 FK,其中只有一个有值?

How can a table have two nullable FKs, where only one of them has a value?

我有一个 Project,可以由组织的 Member 拥有,也可以由 ExternalResourcePerson 拥有。这意味着每个 FK MemberIdExternalResourcePersonId 都可以为空,但不能同时为空。我该如何建立这种关系?

我刚刚开始将术语 "External Resource Person" 引入其中。也许 ExternalResourcePerson-class 应该有一个 List<Project> 而不是 List<ExternalResourcePersonEngagement>?

这些是 classes:

public class Project
{
    public int Id { get; set; }

    // A Project can either have a Member as owner ...
    public int MemberId { get; set; }
    public Member ProjectOwner { get; set; }

    // ... or an ExternalResourcePerson
    public int ExternalResourcePersonId { get; set; }
    public ExternalResourcePerson ExternalProjectOwner { get; set; }
}

public class Member
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Zip { get; set; }

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

public class ExternalResourcePerson
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Organisation { get; set; }
    public string Expertise { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Notes { get; set; }
    public List<ExternalResourcePersonEngagement> Engagements { get; set; }
}

public class ExternalResourcePersonEngagement
{
    public int Id { get; set; }
    public int ProjectId { get; set; }
    public Project Project { get; set; }
    public int ExternalResourcePersonId { get; set; }
    public ExternalResourcePerson Person { get; set; }
}

我已经看到 this question,但我无法将任何答案应用到我的案例中。

只需将它们声明为可为 null 并添加负责正确分配它的方法。

一些不是生产就绪解决方案的基本示例:

//Marker
public interface IProjectOwner
{

}

public class Project
{
    public int Id { get; set; }

    // A Project can either have a Member as owner ...
    public int? MemberId { get; private set; }
    public Member ProjectOwner { get; private set; }

    // ... or an ExternalResourcePerson
    public int? ExternalResourcePersonId { get; private set; }
    public ExternalResourcePerson ExternalProjectOwner { get; private set; }

    public void SetProjectOwner(IProjectOwner projectOwner)
    {    
        //Add some validation for this. Like checking if the project owner is set.
        if(projectOwner == null)
            throw new Exception("Project owner cannot be null.");

        if(projectOwner is Member )
        {
            ProjectOwner = projectOwner;
            MemberId = projectOwner.Id;
        }

        if(projectOwner is ExternalResourcePerson)
        {
            ExternalProjectOwner = projectOwner;
            ExternalResourcePersonId = projectOwner.Id;
        }
    }
}

public class Member : IProjectOwner
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Zip { get; set; }

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

public class ExternalResourcePerson : IProjectOwner
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Organisation { get; set; }
    public string Expertise { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Notes { get; set; }
    public List<ExternalResourcePersonEngagement> Engagements { get; set; }
}

public class ExternalResourcePersonEngagement
{
    public int Id { get; set; }
    public int ProjectId { get; set; }
    public Project Project { get; set; }
    public int ExternalResourcePersonId { get; set; }
    public ExternalResourcePerson Person { get; set; }
}

您还可以添加一些方法来检索此人。