如果我有以下模型,我是否正確使用InverseProperty?實體框架InverseProperty註釋用法
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 {}
這作品時,我不使用InverseProperty
但問題是,數據庫中創建複製PersonID columns
(像他們一樣的4,貌似1,從愛好繼承每種類型的),我不希望這樣,
然而,當我使用InversePorperty
我得到一個異常:
Schema specified is not valid. Errors: The relationship 'x.x.Person_Reading' was not loaded because the type 'x.x.Hobbies' is not available.
您尚未將Hobbies上的Person屬性的外鍵指定爲personID。也許這是造成這個問題的原因。添加'[ForeignKey(「PersonID」)]'屬性。 – 2014-10-03 13:25:11
@ odyss-jii會試着讓你知道,謝謝! – 2014-10-03 13:25:49
@ odyss-jii我得到了同樣的例外 – 2014-10-03 13:27:37