I have two classes, Company and CompanyAddress.
For every Company there can be multiple CompanyAddresses, such as Invoice address and correspondence address.
Via entity framework, I'm trying to include all CompanyAddresses that belong the Company. However, I keep getting the following debug error:
An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code Additional information: A specified Include path is not valid. The EntityType 'StatelyTechAdmin.Models.Company' does not declare a navigation property with the name 'CompanyAddresses'.
Can anyone advise me on why I cannot include all addresses in my LINQ query? Please note I am very new to Entity Framework.
Company Class
public class Company
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public virtual long CompanyId { get; set; }
[Required]
[Display(Name = "Company Name")]
public virtual string Name { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual IEnumerable<CompanyAddress> CompanyAddresses { get; set; }
}
CompanyAddress Class
public class CompanyAddress
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public virtual long CompanyAddressId { get; set; }
[Required]
public virtual long CompanyId { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
[Required]
public virtual int CompanyAddressTypeId { get; set; }
[ForeignKey("CompanyAddressTypeId")]
public virtual CompanyAddressType CompanyAddressType { get; set; }
[Display(Name = "Address 1")]
public virtual string Address1 { get; set; }
[Display(Name = "Address 2")]
public virtual string Address2 {get; set; }
[Display(Name = "Town")]
public virtual string Town { get; set; }
[Display(Name = "City")]
public virtual string City { get; set; }
[Required]
public virtual long CountyId { get; set; }
[ForeignKey("CountyId")]
[Display(Name = "County")]
public virtual County County { get; set; }
[Required]
[Display(Name = "Postal Code")]
public virtual string PostalCode { get; set; }
public virtual DateTime CreatedDate { get; set; }
}
Calling Method from MVC Action
public ActionResult Index()
{
var comp = db.Companies.Include(a => a.CompanyAddresses).ToList();
return View(comp);
}
All advice welcome! Thank you for your time.