使用两个FK时无法创建关系 [英] Cannot create a relationship when using two FK
问题描述
我在我的 ASP.Net Core
应用程序中使用 EF
,并且尝试将 UserNotification
表添加到我的 User
表中。这些是表结构:
I'm using EF
in my ASP.Net Core
application and I'm trying to associate an UserNotification
table to my User
table. These are the tables structure:
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
UserNotifications
中的code>,我将存储 User
已收到的所有通知。
What I did is create a ForeignKey
of UserNotifications
which I'm going to store all the notifications that the User
has reiceved.
在表 UserNotifications
内,我创建了两个 FK
用户
和发件人
。本质上,我想存储已收到通知的 User
的 Id
和 Id已发送通知的
(用户
的发件人
)。
Inside the table UserNotifications
I created two FK
for User
and Sender
. Essentially I want store the Id
of the User
that has reiceved the notification, and the Id
of the User
that has sent the notification (Sender
).
在 OnModelCreating
内,我还定义了以下逻辑:
Inside the OnModelCreating
I also defined the following logic:
builder.Entity<UserNotifications>(entity =>
{
entity.HasKey(n => n.Id);
entity.HasOne(u => u.User)
.WithOne(u => u.UserNotifications)
.HasForeignKey<User>(u => u.Id);
entity.HasOne(u => u.Sender)
.WithOne(u => u.UserNotifications)
.HasForeignKey<User>(u => u.Id);
});
当我在控制台
:
add-migration InitialMigration -context MyAppContext
我得到:
无法在'User.UserNotifications'和'UserNotifications之间建立关系.Sender,因为 UserNotifications.User和 User.UserNotifications之间已经存在关系。导航属性只能参与单个关系。
Cannot create a relationship between 'User.UserNotifications' and 'UserNotifications.Sender', because there already is a relationship between 'UserNotifications.User' and 'User.UserNotifications'. Navigation properties can only participate in a single relationship.
我是 EntityFramework $ c的新手$ c>,所以我不知道该如何解决,有人可以解释我做错了吗?
I'm a newbie of EntityFramework
so I don't know how to fix this, someone could explain what I did wrong?
在此先感谢您的帮助。
推荐答案
您所描述的模型表示用户之间的两个一对多关系
和 UserNotifications
(顺便说一下,该实体应命名为 UserNotification
)实体。每个EF关系可以在每一侧映射到0或1个唯一的导航属性。
The model you are describing represents two one-to-many relationships between the User
and UserNotifications
(btw, the entity should be named UserNotification
) entities. Each EF relationship can be mapped to 0 or 1 unique navigation properties at each side.
您已经有两个 User
和发件人
在 UserNotifications
中引用导航属性(和相应的FK)。您需要的是 User
中的两个对应的集合导航属性:
You already have two User
and Sender
reference navigation properties (and corresponding FKs) in UserNotifications
. What you need is two corresponding collection navigation properties in 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进行映射:
and map the with fluent 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);
});
请注意,由于该模型引入了所谓的多个级联路径,需要关闭至少一个层叠删除并手动进行处理。
Note that since such model introduces the so called multiple cascade paths, you need to turn at least one of the cascade delete off and handle it manually.
这篇关于使用两个FK时无法创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!