简体   繁体   English

EF核心自我引用多对多

[英]EF Core self referencing many to many

I have User table and I'd like to add connection called UserFriend between 2 users. 我有User表,我想在2个用户之间添加名为UserFriend的连接。 I've searched a lot and basicly tried many different solutions and none of them worked. 我进行了很多搜索,基本上尝试了许多不同的解决方案,但没有一个起作用。 Everytime I get same error: 每当我得到相同的错误:

Introducing FOREIGN KEY constraint 'FK_UserFriends_Users_Friend2Id' on table 'UserFriends' may cause cycles or multiple cascade paths. 在表“ UserFriends”上引入FOREIGN KEY约束“ FK_UserFriends_Users_Friend2Id”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Here are my models: 这是我的模型:

public class User
    {
        [Key]
        public Guid Id { get; set; }

        public string Username { get; set; }
        public string EmailAddress { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }

        public virtual ICollection<UserFriend> Friends { get; set; }
        public virtual ICollection<UserFriend> FriendOf { get; set; }               
    }

public class UserFriend
    {                        
        public User Friend1 { get; set; }
        public Guid Friend1Id { get; set; }

        public User Friend2 { get; set; }
        public Guid Friend2Id { get; set; }

        public bool Confirmed { get; set; }
        public DateTime Added { get; set; }
    }

And here's code in DataContext: 这是DataContext中的代码:

modelBuilder.Entity<UserFriend>().HasKey(sc => new { sc.Friend1Id, sc.Friend2Id });            

            modelBuilder.Entity<UserFriend>()
                .HasOne(c => c.Friend1)
                .WithMany(c => c.FriendOf)
                .HasForeignKey(f => f.Friend1Id);                

            modelBuilder.Entity<UserFriend>()
                .HasOne(c => c.Friend2)
                .WithMany(c => c.Friends)
                .HasForeignKey(f => f.Friend2Id)
                .OnDelete(DeleteBehavior.Restrict);

Change your code to below and remove the other lines you have posted. 将您的代码更改为下面,并删除您已发布的其他行。

public class User
    {
        [Key]
        public Guid Id { get; set; }

        public string Username { get; set; }
        public string EmailAddress { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }

        public virtual ICollection<UserFriend> Friends { get; set; }
        public virtual ICollection<UserFriend> FriendOf { get; set; }               
    }

public class UserFriend
    {        

        public User Friend1 { get; set; }
        [ForeignKey("Friend1")]  
        public Guid? Friend1Id { get; set; }

        public User Friend2 { get; set; }
         [ForeignKey("Friend2")]
        public Guid? Friend2Id { get; set; }

        public bool Confirmed { get; set; }
        public DateTime Added { get; set; }
    }

 modelBuilder.Entity<User>();
 modelBuilder.Entity<UserFriend>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM