EF代码首先,表关系问题

发布于 2025-02-02 15:15:20 字数 2685 浏览 1 评论 0原文

我在评论中遇到了一个错误在关系中,我正在等待一个实用的答案,似乎没有困难。我在文章模型中轻松使用图片模型。我想到了在 auhor 模型中使用此表,但是我陷入了关系,该怎么办?

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();
}

这是错误:

引入外键约束'fk_comments_authors_authorid'on 表“评论”可能会导致周期或多个级联路径。指定 在删除无操作或更新中,无操作或修改其他外国 关键约束。无法创建约束或索引。请参阅上一个 错误

在其他关系中有任何问题吗?我们可以讨论

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文