我正在尝试在我的模型中创建以下约束,以便 Tag 对象的 TagType 有效。有效的 TagType 是 OperatingCompanyId 与标签网站的 OperatingCompanyId 匹配的类型。我意识到这似乎令人费解,但从商业角度来看这是有道理的:
运营公司有网站。网站包含标签。标签有一个 TagType(单数)。TagType 在运营公司中是相同的,这意味着如果一家运营公司有 20 个 TagType 和 5 个 WebSite,那么这 20 个 TagType 应该能够在所有 5 个 WebSite 中使用。我想确保标签的 TagType 不能与另一个 OperatingCompany 相关联。
在模型中创建此约束的最佳方法是什么?我需要更改我的 POCO,还是使用 Fluent API?
提前致谢!
[Table("OperatingCompanies")]
public class OperatingCompany : ConfigObject
{
public OperatingCompany()
{
WebSites = new List<WebSite>();
}
[Required(ErrorMessage = "Name is a required field for an operating company.")]
[MaxLength(100, ErrorMessage = "Name cannot exceed 100 characters.")]
public string Name { get; set; }
public virtual ICollection<WebSite> WebSites { get; set; }
}
[Table("Websites")]
public class WebSite : ConfigObject
{
public WebSite()
{
WebObjects = new List<WebObject>();
}
[Required(ErrorMessage = "URL is a required field for a web site.")]
[MaxLength(100, ErrorMessage = "URL cannot exceed 100 characters for a web site.")]
[RegularExpression(@"\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]", ErrorMessage = "The value entered is not a valid URL.")]
public string Url { get; set; }
public OperatingCompany OperatingCompany { get; set; }
[Required(ErrorMessage = "You must associate a web site with an operating company.")]
public Guid OperatingCompanyId { get; set; }
[InverseProperty("Website")]
public virtual ICollection<WebObject> WebObjects { get; set; }
}
[Table("Tags")]
public class Tag : ConfigObject
{
[Required(ErrorMessage = "Name is a required field for a tag.")]
[MaxLength(100, ErrorMessage = "Name cannot exceed 100 characters for a tag.")]
public string Name { get; set; }
public TagType TagType { get; set; }
[Required(ErrorMessage = "You must associate a tag with a tag type.")]
public Guid TagTypeId { get; set; }
public WebSite WebSite { get; set; }
[Required(ErrorMessage = "You must associate a tag with a web site.")]
public Guid WebSiteId { get; set; }
}
[Table("TagTypes")]
public class TagType : ConfigObject
{
[Required(ErrorMessage = "Name is a required field for a tag.")]
[MaxLength(100, ErrorMessage = "Name cannot exceed 100 characters for a tag type.")]
public string Name { get; set; }
public OperatingCompany OperatingCompany { get; set; }
[Required(ErrorMessage = "You must associate a tag type with an operating company.")]
public Guid OperatingCompanyId { get; set; }
}