配置关系,所以我有这些模型:在C#代码首先
ParentModel.cs
public class ParentModel {
...properties here
//relationship to Child
[ForeignKey("ChildModelID")]
public ChildModel ChildModel { get; set; }
}
ChildModel.cs
public class ChildModel{
...properties here
//relationship to parent
[ForeignKey("ParentModel")]
public int? ParentModelID { get; set; }
public ParentModel ParentModel { get; set; }
}
我使用的Web API代码优先。我是新来的。我在创建数据库时遇到以下错误。但是,如果我删除ParentModel中的导航属性(即public ChildModel ChildModel { get; set; }
),一切正常,但它不能满足我的需求。当我获得ParentModel时,我想要加载(或延迟加载不知道哪个项是正确的)ChildModel。这里的关系是零或一对一。意思是ChildModel可以有一个记录,在ParentModel中没有引用。
Multiplicity is not valid in Role 'ChildModel_ParentModel_Source' in relationship
'ChildModel_ParentModel'. Because the Dependent Role properties are not the key
properties, the upper bound of the multiplicity of the Dependent Role must
be '*'.
预先感谢您!
怎么样删除[ForeignKey的( “ChildModelID”)]来自ParentModel? – yyou
仍然是一样的,原来那个代码不在那里,但在线回答给我的建议,但仍然不起作用。 – Jed
[EF6外键有一对一关系和双向属性导航]的可能重复(https://stackoverflow.com/questions/42515355/ef6-foreign-key-with-one-to-one-relationship -and-two-way-property-navigation) –