如果我有以下模型,我是否正确使用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