Csharp/C#教程:如何与Entity Framework Code First建立一对一的关系分享


如何与Entity Framework Code First建立一对一的关系

我一直在阅读有关在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); 

执行时, Account上的AccountConfig属性为NULLAccountConfig上的Account属性是不正确的记录(巧合的是,检索到的Account.IdAccountConfig.Id相同,但我不知道这是否意味着什么)。

在数据库中, Account表没有对AccountConfig记录的引用,但AccountConfig表使用AccountId列引用了Account记录。

最终结果是我可以从Account引用AccountConfig ,并且(如果可能的话)引用AccountConfig Account

使用EF,仅在共享主键的表上支持一对一关系。 您的AccountConfig表具有自己的主键和Account的外键。 EF仅支持与此配置的一对多关系。

本文对EF中的1:1和1:0..1关系进行了很好的解释。 这里的限制是EF不支持唯一约束这一事实的副产品。

上述就是C#学习教程:如何与Entity Framework Code First建立一对一的关系分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1033806.html

(0)
上一篇 2022年1月18日
下一篇 2022年1月18日

精彩推荐