我有一个Project
,它可以由组织的Member
拥有,也可以由ExternalResourcePerson
拥有。这意味着FKMemberId
或ExternalResourcePersonId
中的每一个都可以为null,但不能同时为null。我该如何建立这种关系?
我刚开始在这里引入"外部资源人员"这个术语。也许ExternalResourcePerson
-类应该有一个List<Project>
而不是List<ExternalResourcePersonEngagement>
?
这些是类别:
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; }
}
我看到了这个问题,但我无法将任何答案应用于我的案例。
只需将它们声明为可为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; }
}
您还可以添加一些方法来检索此人。