导航属性不适用于可选的外键

时间:2019-09-17 16:02:23

标签: entity-framework entity-framework-core

我试图设计具有两个实体的数据库模式:BContract和Subscription。订阅可以不存在BContract而存在,因此BContract ID是可选的。

public class BContract
{
    public int Id { get; set; }

    [ForeignKey("Subscription")]
    public int SubscriptionId { get; set; }

    public Subscription Subscription { get; set; }
}

public class Subscription
{
    public int Id { get; set; }

    [ForeignKey("BContract")]
    public int? BContractId { get; set; }

    public BContract BContract { get; set; }
}

当我创建新的BContract时,我设置了订阅ID,并且savechanges成功运行。但是,当我尝试通过订阅获取BContract时,它将为以下两个字段返回空值:BContractId和BContract

为什么在添加具有指定subscriptionId的新BContract时,它不会自动更新Subscription实体的BContractId?

0 个答案:

没有答案