使用两个FK时无法创建关系

时间:2018-10-19 19:09:26

标签: asp.net entity-framework asp.net-core entity-framework-core

我在EF应用程序中使用ASP.Net Core,并且试图将UserNotification表与我的User表关联。这些是表结构:

public class User : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual UserNotifications { get; set; }
}

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

    [Key, ForeignKey("User")]
    public string UserId { get; set; }
    public User User { get; set; }

    [Key, ForeignKey("Sender")]
    public string SenderId { get; set; }
    public virtual User Sender { get; set; }      

    public string Title { get; set; }
    public string Message { get; set; }
}

我所做的是创建一个ForeignKey的{​​{1}},我将存储UserNotifications收到的所有通知。

在表User内,我为UserNotificationsFK创建了两个User。本质上,我想存储已收到通知的Sender的{​​{1}}和已发送通知(Id的{​​{1}}的{​​{1}}。

User中,我还定义了以下逻辑:

Id

当我在User中键入以下建筑物时:

Sender

我得到:

  

无法在“ User.UserNotifications”和“ UserNotifications.Sender”之间创建关系,因为“ UserNotifications.User”和“ User.UserNotifications”之间已经存在关系。导航属性只能参与单个关系。

我是OnModelCreating的新手,所以我不知道该如何解决,有人可以解释我做错了什么?

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

您要描述的模型表示UserUserNotifications之间的一对多关系(两个)(顺便说一句,该实体应命名为UserNotification)实体。每个EF关系都可以在每一侧映射到0或1个唯一的导航属性。

您已经在User中拥有两个 SenderUserNotifications参考导航属性(以及相应的FK)。您需要在User两个对应的集合导航属性:

public class User : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<UserNotifications> ReceivedNotifications { get; set; }
    public virtual ICollection<UserNotifications> SentNotifications { get; set; }
}

并使用流畅的API映射

builder.Entity<UserNotifications>(entity =>
{
    entity.HasKey(n => n.Id);

    entity.HasOne(n => u.User)
        .WithMany(u => u.ReceivedNotifications)
        .HasForeignKey(n => u.UserId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Delete);

    entity.HasOne(n => n.Sender)
        .WithMany(u => u.SentNotifications)
        .HasForeignKey(n => n.SenderId)
        .IsRequired()
        .OnDelete(DeleteBehavior.Restrict);
 });

请注意,由于此类模型引入了所谓的多个级联路径,因此您需要关闭至少一个级联删除并手动进行处理。

相关问题