1

I try to setup a class which is UserUserFavourite. It will store the relationship between users if they are following users or followed by users.

To do that I created a class as below:

 [Table("UserUserFavorite")]
public class UserUserFavorite
{
    [ForeignKey("User1")]
    public long UserId1 { get; set; }

    public User User1 { get; set; }

    [ForeignKey("User2")]
    public long UserId2 { get; set; }
    public User User2 { get; set; }
}

The user1 is following user2.

In my UserClass I have tow properties:

`public List<UserUserFavorite> Followers{ get; set; }
 public List<UserUserFavorite> Followings{ get; set; }`

In my dbcontext I have:

modelBuilder.Entity<UserUserFavorite>().HasKey(sc => new { sc.UserId1, sc.UserId2 });

Then I generate my migration I have the error:

Unable to determine the relationship represented by navigation property 'UserUserFavorite.User1' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

What's wrong?

dalton5
  • 915
  • 2
  • 14
  • 28
  • Would it make sense to rename `User1` and `User2` to something which is better to understand? What is the `User1` and why isn't it named that way? – Progman May 17 '20 at 18:28
  • User1 is following User2. User2 is followed by User1. – dalton5 May 17 '20 at 18:29
  • Please [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. Specially the modeling part where you define the relationship between the entities. Also see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query for SQL related questions. – Progman May 17 '20 at 18:42

1 Answers1

1

You need to set DeleteBehavior.Restrict in OnModelCreating.

Here is the same case as your requirement, you can refer to this.

I have created a demo based on your code:

 public class UserUserFavorite
    {
        public long UserId1 { get; set; }

        public User User1 { get; set; }

        public long UserId2 { get; set; }

        public User User2 { get; set; }
    }

    public class User
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public UserUserFavorite userUserFavorite1 { get; set; }
        public UserUserFavorite userUserFavorite2 { get; set; }
    }

OnModelCreating:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {  
            modelBuilder.Entity<UserUserFavorite>().HasKey(sc => new { sc.UserId1, sc.UserId2 });
            modelBuilder.Entity<UserUserFavorite>()
                   .HasOne(m => m.User1)
                   .WithOne(t => t.userUserFavorite1)
                   .HasForeignKey<UserUserFavorite>(m => m.UserId1)
                   .OnDelete(DeleteBehavior.Restrict); 
            modelBuilder.Entity<UserUserFavorite>()
                        .HasOne(m => m.User2)
                        .WithOne(t => t.userUserFavorite2)
                        .HasForeignKey<UserUserFavorite>(m => m.UserId2)
                        .OnDelete(DeleteBehavior.Restrict); 
}
LouraQ
  • 6,443
  • 2
  • 6
  • 16