EF中最简单的朋友列表方法是什么? [英] What's the simplest approach to friends list in EF?

查看:94
本文介绍了EF中最简单的朋友列表方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的朋友列表,每行都有两个id(用户ID和朋友ID),两个引用相应用户个人资料中的id字段

I would like to create a simple friend list, with each row having two id's (user id and friend id), both referencing "id" field in respective user's profile

| relationId | userId | friendId |

|relationId|userId|friendId|

如果双方向彼此发送友谊请求,使用户成为朋友。例如:

| 1 | 2 | 4 |

| 1 | 4 | 2 | // id = 2和4的用户是朋友

So that users become friends if both sides send a friendship request to eachother. For example:
|1|2|4|
|1|4|2| //users with id 2 and 4 are friends

到目前为止我设置一个外键很简单。当我必须在同一张桌子上设置两个外键时,事情变得更加困难。我正在收到不同的错误,我想修复,然后重新调整我的代码,最后我觉得我已经过度复述了。

Setting up a foreign key has been easy for me so far. Things get harder when I have to set up two foreign keys to the same table. I am getting different errors which I look to fix and then re-adjust my code and in the end I feel like I am overcomplicating this.

UserProfile模型:

UserProfile model:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
}

朋友模特:

[Table("Friends")]
public class Friends
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int RelationId { get; set; }

    public int UserId { get; set; }

    [ForeignKey("UserId")]
    public UserProfile User { get; set; } // works with one foreign key

    public int FriendId { get; set; }

    [ForeignKey("FriendId")]
    public UserProfile Friend { get; set; } // doesn't work when I add this one
}

这给我以下例外:


在表朋友上引入FOREIGN KEY约束Friends_User可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Introducing FOREIGN KEY constraint 'Friends_User' on table 'Friends' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

我搜索过类似的问题,我有看到很多外键的例子,但是,我找不到任何与同一个表相连的两个字段的例子。

I have searched for similar problems, I have seen examples with many foreign keys, however, I wasn't able to find any example with two fields linked to the same table.

推荐答案

在这种情况下,异常消息告诉您有多个具有级联操作的外键。这是因为默认情况下,EF需要关联删除。

In this case, the exception message tells you that there are multiple foreign keys with cascade actions. This is because with required associations EF implements cascade on delete by default.

所以你必须从关联中删除至少一个级联操作。没有数据注释,所以您应该使用流畅的映射,例如:

So you have to remove at least one cascade action from an association. There is no data annotation for this, so you should use fluent mapping, for instance:

modelBuilder.Entity<Friends>()
            .HasRequired(p => p.User)
            .WithMany()
            .HasForeignKey(p => p.UserId)
            .WillCascadeOnDelete(true);
modelBuilder.Entity<Friends>()
            .HasRequired(p => p.Friend)
            .WithMany()
            .HasForeignKey(p => p.FriendId)
            .WillCascadeOnDelete(false);

(在 DbContext 的覆盖 OnModelCreating )。

您不需要 ForeignKey 属性当您使用此映射时,

You don't need the ForeignKey attributes anymore when you use this mapping.

这篇关于EF中最简单的朋友列表方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆