设置两个具有不同名称的外键

时间:2017-12-24 16:39:14

标签: c# entity-framework entity-framework-core

我有一个Offer实体,它在我的系统中的两个用户之间提供链接。

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }

    public virtual User Provider { get; set; }
    public virtual User Receiver { get; set; }

}

User实体的主键为:

public string Id { get; set; }

我希望这两个用户ID字符串出现在Offer表中,如下所示:

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }
    public string ProviderId { get; set; }
    public string ReceiverId { get; set; }

    public virtual User Provider { get; set; }
    public virtual User Receiver { get; set; }

}

我试过了:

public class Offer
{
    public OfferStatus Status { get; set; }
    public decimal Amount {get; set; }
    public string ProviderId { get; set; }
    public string ReceiverId { get; set; }

    [ForeignKey(nameof(ProviderId))]
    public virtual User Provider { get; set; }
    [ForeignKey(nameof(ReceiverId))]
    public virtual User Receiver { get; set; }

}

这就是我通常设置外键的方式,但通常我只有其中一个,我使用EF提供的命名约定魔法,所以我知道我在这里所做的是错的,但我很困惑,我应该如何设置他们的外键属性。

0 个答案:

没有答案