if I have the following models, am I using the InverseProperty correctly?
class Person {
public int PersonID {get;set;}
[InverseProperty("Person")]
public List<Hobbies> Sports {get;set;}
[InverseProperty("Person")]
public List<Hobbies> Art {get;set;}
[InverseProperty("Person")]
public List<Hobbies> Reading {get;set;}
}
abstract class Hobbies {
public int HobbiesID {get;set;}
public string HobbyName {get;set;}
public int HobbyRating {get;set;}
public int PersonID {get;set;}
public Person Person {get;set;}
}
class Sports : Hobbies {}
class Art : Hobbies {}
class Reading : Hobbies {}
this works when I don't use the InverseProperty
but the problem is that the database creates duplicate PersonID columns
(like 4 of them, looks like 1 for each type that inherits from Hobbies), and I don't want that,
However, when I use InversePorperty
I get an exception:
Schema specified is not valid. Errors: The relationship 'x.x.Person_Reading' was not loaded because the type 'x.x.Hobbies' is not available.
First, This looks strange : you are creating multiple mapping to the same object qwith different names ?
It should be something like :
If you set the property Person in the abstract class, then the mapping must be to the abstract class.
Otherwise, you have to declare the PersonId in the abstract class and then set the Person property in every concrete class using the attribute [ForeignKey("PersonId")]. But this solution is pretty strange.
Secondly, if you want to specify the ForeignKey for the Person, you should use :
Third : Are you sure you don't need M-N relation? Do you really want every people to create new Hobbies (you would finally have multiple times "Driving" (or whatever) as a hobby).