我想创建一个简单的好友列表,每行有两个ID的(用户ID和朋友id),都引用“ID”,在相应的用户配置文件字段在EF中的朋友列表最简单的方法是什么?
| relationId |用户id | friendId |
让用户成为朋友,如果双方都向友人发送友谊请求。例如:
| 1 | 2 | 4 |
| 1 | 4 | 2 | // ID为2和4的用户是朋友
到目前为止,设置外键对我来说很简单。当我必须将两个外键设置到同一个表时,事情会变得更加困难。我得到了不同的错误,我期望解决这个问题,然后重新调整我的代码,最后我觉得我太过于复杂了。
用户配置模式:
[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约束。
我已经搜索了类似的问题,我看到了很多外键的例子,但是,我无法找到任何两个字段链接到同一个表的示例。
这正是我一直在寻找的。猜猜这不能简单地与EF ..我提到它的作品像一个魅力? –