我有一个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提供的命名约定魔法,所以我知道我在这里所做的是错的,但我很困惑,我应该如何设置他们的外键属性。