我一直在阅读关于在EF
中建立一对一关系的人的所有Google和SO页面,但它对我而言不起作用。如何建立与实体框架代码的第一对一关系
这里是我的模型:
帐户:
public int Id {get; set;}
public virtual AccountConfig AccountConfig {get; set;}
账户地图:
HasKey(t => t.Id);
HasRequired(t => t.AccountConfig)
.WithOptional();
帐户配置:
public int Id {get; set;}
public int AccountId {get; set;}
public virtual Account Account {get; set;}
帐户配置地图:
HasKey(t => t.Id);
HasRequired(t => t.Account)
.WithOptional(t => t.AccountConfig);
执行时,AccountConfig
财产上Account
是NULL
和Account
财产上AccountConfig
是不正确的记录(巧合的是,检索Account.Id
是一样的AccountConfig.Id
,但我不知道这意味着什么)。
在数据库中,Account
表没有到AccountConfig
记录的引用,但AccountConfig
表必须使用AccountId
列Account
记录的引用。
最终的结果将是对我从Account
从AccountConfig
具有到AccountConfig
基准和(如果可能),以Account
的参考。
所以我只能从帐户引用AccountConfig表,如果帐户表有一个名为AccountConfigId的列? – jermny 2013-03-21 14:10:55
不完全。你的'AccountConfig'实体应该只有一个'Id'列;删除'AccountId'列并确保'Id'不是自动生成的。当添加'AccountConfig'实体时,他们的'Id's应该明确地设置为与'Account'表记录的'Id'相同。 – Olly 2013-03-21 14:14:54
请参阅我链接的文章,其中的示例与您的示例非常相似。 – Olly 2013-03-21 14:15:34