无法删除有帖子的作者

时间:2019-01-30 14:15:48

标签: c# .net entity

我目前正在开发博客应用程序,偶然发现了以下问题。

我可以删除具有0个博客文章的作者,但不能删除具有博客文章的作者。

  

AuthorController中的HttpPost删除方法

    [HttpPost, ActionName("Delete")]
    public IActionResult DeleteConfirmed(int id)
    {
        if (ModelState.IsValid) {
            try {
                Author author = _authorRepository.GetById(id);
                _authorRepository.Remove(author);
                _authorRepository.SaveChanges();
                TempData["Success"] = $"{author.Firstname} {author.Lastname} has been successfully deleted!";
                return RedirectToAction(nameof(Index));
            }
            catch (Exception ex) {
                ModelState.AddModelError("", ex.Message);
            }
        }
        Author author2 = _authorRepository.GetById(id);
        ViewData["Name"] = $"{author2.Firstname} {author2.Lastname}";
        return View(nameof(Delete));
    }
  

我的作者配置映射

public class AuthorConfiguration : IEntityTypeConfiguration<Author>
{
    public void Configure(EntityTypeBuilder<Author> builder)
    {
        builder.ToTable("Author");
        builder.HasKey(a => a.AuthorId);
        builder.Property(a => a.Firstname).HasMaxLength(40).IsRequired();
        builder.Property(a => a.Lastname).HasMaxLength(40).IsRequired();
        builder.HasMany(a => a.Posts)
            .WithOne()
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);
    }
}
  

作者模型

public class Author
{
    public int AuthorId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public ICollection<Post> Posts { get; set; }
    public int NrOfPosts => Posts.Count;

    protected Author()
    {
        Posts = new List<Post>();
    }

    public Author(string firstname, string lastname) : this()
    {
        Firstname = firstname;
        Lastname = lastname;
    }

    public void AddPost(Post post)
    {
        if (Posts.FirstOrDefault(p => p.PostId == post.PostId) == null) {
            Posts.Add(post);
        }
    }

    public void RemovePost(Post post)
    {
        Posts.Remove(post);
    }
}
  

帖子模型

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime Date { get; set; }
    public Category Category { get; set; }
    public ICollection<PostTag> Tags { get; set; }

    protected Post()
    {
        Tags = new List<PostTag>();
    }

    public Post(string title, string content, Category category) : this()
    {
        Title = title;
        Content = content;
        Date = DateTime.Now;
        Category = category;
    }

    public void AddTag(Tag tag)
    {
        if (FindTag(tag.Name) == null) {
            Tags.Add(new PostTag(this, tag));
        }
    }

    public void RemoveTag(Tag tag)
    {
        PostTag postTag = Tags.FirstOrDefault(pt => pt.Tag == tag);
        Tags.Remove(postTag);

    }

    public Tag FindTag(string name)
    {
        PostTag postTag = Tags.FirstOrDefault(pt => pt.Tag.Name == name);
        return postTag?.Tag;
    }
}

要删除实际上有帖子的作者,我缺少什么?

预先感谢

1 个答案:

答案 0 :(得分:1)

我假设您首先进行模型化实体框架。在这种情况下,您的基础<mat-form-field> <input matNativeControl> & <textarea matNativeControl> <select matNativeControl> <mat-select> <mat-chip-list> <mat-selection-list> is not supported for now. 表中很有可能具有外键约束。尝试删除所有帖子。例如,

brew install python3
brew unlink python

希望这会起作用,或者使您找到一个可行的答案。基本上听起来像是有一个外键约束正在使您绊倒。