How can I set the ForeignKeys (ideally using Data Annotations) in this scenario:
[Table("Teacher", Schema = "Account")]
public partial class Teacher
{
[Key]
public int teacherId { get; set; }
public string name { get; set; }
public virtual Address Address { get; set; }
}
[Table("Student", Schema = "Account")]
public partial class Student
{
[Key]
public int studentId { get; set; }
public string name { get; set; }
public virtual Address Address { get; set; }
}
Both tables Student and Teacher will have one address.
[Table("Address", Schema = "Location")]
public partial class Address
{
[Key]
public int addressId { get; set; }
public string details { get; set; }
public virtual Student Student { get; set; }
public virtual Teacher Teacher { get; set; }
}
Table address should have a constraint to either Teacher OR Student tables, meaning that each row in Address table must be linked to either a Student OR a Teacher.
I just cannot find a way to achieve this. With the current code, when adding migration, I got the error: Unable to determine the principal end of an association between the types 'Student' and 'Address'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.