I have what I figured would be a pretty basic question, but I can't seem to get it working properly. So, I am setting up a MVC 5 project, using EF6 Framework using Model-First. To simplify things, I am going to say I have 4 models: Student, Teacher, Parent, Address. Student, Teacher and Parent share similar fields, so I wanted to create a Person base class to inherit from. I did that no problem, but my problem comes in with relating an address record to a person. I would have a 1 to 1:0 relationship, from person to Address. So, my Address class looks like this..
public class Address
{
public int ID { get; set; }
public string StreetLine1 { get; set; }
public string StreetLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
[Key]
[ForeignKey("Person")]
public int PersonID { get; set; }
public virtual Person person { get; set; }
}
And my Person class looks like this...
public abstract class Person
{
public int ID { get; set; }
[Required]
[StringLength(50,ErrorMessage = "First Name cannot be longer than 50 characters.")]
[Display(Name= "First Name")]
public string FirstName { get; set; }
[StringLength(50, ErrorMessage = "Middle Name cannot be longer than 50 characters.")]
[Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
public PersonType personType {get; set;}
[Key]
[ForeignKey("Address")]
public int AddressID { get; set; }
public virtual Address Address { get; set; }
[Display(Name = "Full Name")]
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
[Display(Name = "Full Name w Middle")]
public string FullNameWMid
{
get
{
return FirstName + " " + MiddleName + " " + LastName;
}
}
}
I have my student class defined as...
public class Student : Person
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:mm-dd-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Birthday")]
public DateTime Birthday { get; set; }
public virtual ICollection<Parent> Parents { get; set; }
public virtual Teacher teacher { get; set; }
}
So, my problem comes in when I try to create a new controller for Student. I am getting an error about unable to retrieve metadata for Student because the ForeignKey PersonID on type Address is not valid. The Navigation type Person was not found on the dependent type Address.
So, I clearly have a Person navigation type in Address, and have a Address navigation type in Person, as well as a FK lookup for AddressID. I'm assuming this has to do with PersonID vs. StudentID because of the inheritance. So, I did try changing to use a Nav Type of Student in Address, and then put an Address Nav Type in Student. When I did that, I get the same exact error as above, in regards to Person/PersonID. I'm sure this is a basic OOO principle that I am missing, but I would appreciate any guidance in figuring out what I did wrong!
Thanks in advance all!
[Key]
on the PersonId when there also is an Address.ID ?