C# ASP.Net MVC 5 EF6-允许两个属性上的null值,但强制至少存在一个属性

C# ASP.Net MVC 5 EF6-允许两个属性上的null值,但强制至少存在一个属性,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,简单地说,我希望ContractDetails的每个条目都分配给一个站点或一个公司,而不是同时分配给两个站点或公司,并且必须选择其中一个,否则根本没有链接。我不太确定如何在实体框架中表示这一点。我目前的模式是: 公司模式: public class Company { public int ID { get; set; } public string Company_Name { get; set; } public string Company_Prefix { get

简单地说,我希望ContractDetails的每个条目都分配给一个站点或一个公司,而不是同时分配给两个站点或公司,并且必须选择其中一个,否则根本没有链接。我不太确定如何在实体框架中表示这一点。我目前的模式是:

公司模式:

public class Company
{
    public int ID { get; set; }
    public string Company_Name { get; set; }
    public string Company_Prefix { get; set; }

    public virtual ICollection<Site> Sites { get; set; }

}
站点模型:

public class Site
{
    public int ID { get; set; }
    public string Site_Name { get; set; }
    public string Site_TelephoneNumber { get; set; }
    public string Site_City { get; set; }
    public int CompanyID { get; set; }

    public virtual Company Company { get; set; }

}
只需在
ContractDetails
类上实现。在
Validate
方法上放置验证逻辑。如果对象无效,则必须返回
ValidationResult
的集合。保存对象时,EF将执行
Validate
方法,并验证您的
ContractDetails
对象是否一致

public class Site
{
    public int ID { get; set; }
    public string Site_Name { get; set; }
    public string Site_TelephoneNumber { get; set; }
    public string Site_City { get; set; }
    public int CompanyID { get; set; }

    public virtual Company Company { get; set; }

}