2

I have User table and I'd like to add connection called UserFriend between 2 users. 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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

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:

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);
Isard
  • 312
  • 1
  • 14
  • Have you tried removing cascade delete? – 3dd Jul 30 '18 at 08:44
  • 1
    Building a database from scratch using your classes/config works fine for me (ef-core 2.1.1) – Gert Arnold Jul 30 '18 at 08:47
  • I can't find any other way to disable cascade delete than this one: https://stackoverflow.com/questions/49326769/entity-framework-core-delete-cascade-and-required And it doesn't help too. – Isard Jul 30 '18 at 09:06

1 Answers1

1

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>();
tech-y
  • 1,829
  • 2
  • 16
  • 26
  • So why do I get "Unable to determine the relationship represented by navigation property 'User.Friends' " ? – Gert Arnold Jul 30 '18 at 10:40
  • I have added the model builder lines as well, which I omitted earlier. – tech-y Jul 30 '18 at 11:03
  • 1
    I get this exception. On top of that, how would EF know the connection between `Friend1` and `Friends`, and `Friend2` and `FriendOf`? If finally you get all this working with InverseProperty and all, you'd get the cascade cycle error. And did you try OP's code? – Gert Arnold Jul 30 '18 at 11:30