I am trying to create the following constraint in my model so that a Tag object's TagType is valid. A valid TagType is one whose OperatingCompanyId matches the Tag's Website's OperatingCompanyId. I realize that this seems convoluted however it makes sense from a business standpoint:
An Operating Company has WebSites. Websites contain Tags. Tags have a TagType(singular). TagTypes are the same across Operating Companies, meaning that if one Operating Company has twenty TagTypes and five WebSites, those twenty TagTypes should be able to be used across all fives of those WebSites. I want to ensure that a Tag's TagType cannot be one associated with another OperatingCompany.
What is the best way to create this constraint in the model? Do I need to change my POCO, or use the Fluent API?
Thanks in advance!
[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; }
}