0

I am trying in .NET EFCore the following Code-First migrations through the entities below

User

[Table("Users")]
public class User
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(100)]
    public string FirstName { get; set; }

    [Required]
    [MaxLength(100)]
    public string LastName { get; set; }

    [Required]
    [MaxLength(250)]
    public string Email { get; set; }

    [Required]
    [MinLength(8), MaxLength(16)]
    public string Password { get; set; }

    [Required]
    [MinLength(6), MaxLength(15)]
    public string Phone { get; set; }

    public ICollection<Apartment> Apartments { get; set; }

    public ICollection<Rating> Ratings { get; set; }
}

Apartment

[Table("Apartments")]
public class Apartment
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MinLength(24), MaxLength(100)]
    public string Title { get; set; }

    [Required]
    [MinLength(24), MaxLength(250)]
    public string Address { get; set; }

    [Required]
    public int Price { get; set; }

    [ForeignKey("User")]
    public int UserId { get; set; }

    public User User {get; set;}

    public ICollection<Rating> Ratings { get; set; }

    public ICollection<AptCateg> AptsCategs { get; set; }
}

Ratings

[Table("Ratings")]
public class Rating
{
    [Key]
    public int Id { get; set; }

    public int Value { get; set; }

    [ForeignKey("Apartment")]
    public int ApartmentId { get; set; }

    public Apartment Apartment { get; set; }

    [ForeignKey("User")]
    public int UserId { get; set; }

    public User User { get; set; }
}

I use the commands dotnet ef migrations add InitialDatabase but when I try to use dotnet ef database update it throws the following error in cmd, as in the title

'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or multiple cascade paths

I tried adding as in the EFCore tutorial from here the modelBuilder's Cascade behavior but it doesn't work because I am getting the same error. I have also tried doing the answer from here but the implementation for HasRequired isn't working even if try to install EntityFrameworkCore.Tools.

I understand that there is an issue with a circular thingy going on. From my intuition the program doesn't know what to do in the case of deleting a user, if to drop or not its ratings and apartments or some of that sort, and this is why its acting this way but I can't fix the problem.

My question is, how can I solve this issue as I cannot create my database, and thus I cannot continue working on the project.

Thanks!

Vlădel
  • 212
  • 1
  • 5
  • 15

2 Answers2

2

You'll have to make the user relationship optional on one of the tables like:

public int? UserId { get; set; }

Making the property type nullable tells EF that a cascade delete is not required here.

juunas
  • 41,356
  • 5
  • 86
  • 118
  • This works. I've read something, somewhere, about making the FK nullable but I wasn't sure about it. Should have tried from the beginning but at least other people will see this question, and get their issue solved as too. Thanks a lot! Marked as answer. – Vlădel Sep 13 '18 at 06:23
0

You are causing a cyclic reference by adding the User and Apartment to the Ratings entity. User and Apartment entities already have a one-to-many relationship to the Ratings collection.

'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or multiple cascade paths

This is how the Ratings entity should look like:

[Table("Ratings")]
public class Rating
{
    [Key]
    public int Id { get; set; }

    public int Value { get; set; }
}
  • @vladel let me know if my suggestion fixes the EF error. – Mihaela Diaconu Sep 12 '18 at 14:55
  • Why would the Rating table should be like this? It wouldn't have any connection with the other tables if it doesn't implement navigation properties at least. I need the Ratings table because a User can leave a rating on an Apartment and I need to determine if he rated or not an apartment in order to prevent him from over-rating. Thanks any way for the help. – Vlădel Sep 13 '18 at 06:22