0

I get an error in the comments model in relations, I'm waiting for a practical answer, it seems like there is nothing difficult. I use the picture model easily in the article model. I thought of using this table in the auhor model, but I am stuck in relationships, what should I do?

public class Article
{
    public int Id { get; set; }
    public string Title { get; set; } = string.Empty;
    public string Detail { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public bool IsActive { get; set; } = false;

    // Scaler properties
    public int AuthorId { get; set; }
    public int CategoryId { get; set; }

    // Navigation properties
    public virtual Picture Picture { get; set; } = new Picture();
    public virtual Author Author { get; set; } = new Author();
    public virtual Category Category { get; set; } = new Category();
    public virtual ICollection<Comment>? Comments { get; set; }
}

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Surname { get; set; } = string.Empty;
    public string EMail { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;
    public DateTime DateOfBirth { get; set; }
    public DateTime CreatedAt { get; set; }

    // Navigation properties
    public virtual ICollection<Article>? Articles { get; set; }
    public virtual ICollection<Comment>? Comments { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public byte SortBy { get; set; }

    // Navigation property
    public virtual ICollection<Article>? Articles { get; set; }
}

public class Comment
{
    public int Id { get; set; }
    public string Content { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    public bool IsActive { get; set; } = false;

    // Scaler properties
    public int ArticleId { get; set; }
    public int AuthorId { get; set; }

    // Navigation properties
    public virtual Article Article { get; set; } = new Article();
    public virtual Author Author { get; set; } = new Author();
}

public class Picture
{
    public int Id { get; set; }
    public string Src { get; set; } = string.Empty;
    public string Alt { get; set; } = string.Empty;
    public string? Title { get; set; }

    // Scaler property
    public int ArticleId { get; set; }

    // Navigation property
    public virtual Article Article { get; set; } = new Article();
}

This is the error:

Introducing FOREIGN KEY constraint 'FK_Comments_Authors_AuthorId' on table 'Comments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors

Are there any problems in other relationships? we can discuss

Nemanja Todorovic
  • 2,521
  • 2
  • 19
  • 30
Ejd3rE
  • 1
  • What errors are you seeing? When do they happen - can you provide a simple code example that illustrates when the error occurs? – RikRak May 30 '22 at 08:23
  • Don't initialize reference navigation properties. https://stackoverflow.com/a/20773057/861716 That said, you should describe the problem more clearly ("but I am stuck in relationships" isn't clear). – Gert Arnold May 30 '22 at 08:25
  • Introducing FOREIGN KEY constraint 'FK_Comments_Authors_AuthorId' on table 'Comments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors. – Ejd3rE May 30 '22 at 08:29
  • Well, that's a clear exception message that gives you enough hints how too solve it. – Gert Arnold May 30 '22 at 08:35
  • 1
    @GertArnold *but I am stuck in relationships* - one of the downsides of polyamory indeed! :D – Caius Jard May 30 '22 at 08:41
  • Tıss oke i ll try another one – Ejd3rE May 30 '22 at 08:49
  • `Article.Picture` seems wrong; should it be a collection? – Caius Jard May 30 '22 at 08:54
  • builder.HasOne(comment => comment.Author) .WithMany(author => author.Comments) .OnDelete(DeleteBehavior.Restrict); this solved the problem. Well, I want to use the picture table together, what should I do in my article and author table? – Ejd3rE May 30 '22 at 09:10
  • I gave article and picture 1 to 1 relationship.I want to use the same image model in my author model, is it the right way? – Ejd3rE May 30 '22 at 09:14

0 Answers0