无法删除有帖子的作者
Can not delete Authors that have Posts
我目前正在开发一个博客应用程序,偶然发现了以下问题。
我可以删除拥有 0 个博文的作者,但不能删除拥有博文的作者。
My HttpPost delete method in AuthorController
[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));
}
My authorconfiguration mapping
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);
}
}
Author model
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);
}
}
Post model
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;
}
}
删除实际有帖子的作者我错过了什么?
提前致谢
我假设你先做模型 Entity Framework。如果是这种情况,您很可能在基础 Post
table 中有一个外键约束。也尝试删除所有帖子。例如,
if (ModelState.IsValid) {
try {
Author author = _authorRepository.GetById(id);
var postIds = author.Posts.Select(post => post.Id);
// Loop through posts and remove
foreach (var postId in postIds) {
var post = _postRepository.GetById(postIds);
_postRepository.Remove(post);
}
// Done with posts
_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);
}
希望这会奏效,或者让您找到可行的答案。基本上,这听起来好像有一个外键约束让您感到困惑。
我目前正在开发一个博客应用程序,偶然发现了以下问题。
我可以删除拥有 0 个博文的作者,但不能删除拥有博文的作者。
My HttpPost delete method in AuthorController
[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));
}
My authorconfiguration mapping
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);
}
}
Author model
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);
}
}
Post model
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;
}
}
删除实际有帖子的作者我错过了什么?
提前致谢
我假设你先做模型 Entity Framework。如果是这种情况,您很可能在基础 Post
table 中有一个外键约束。也尝试删除所有帖子。例如,
if (ModelState.IsValid) {
try {
Author author = _authorRepository.GetById(id);
var postIds = author.Posts.Select(post => post.Id);
// Loop through posts and remove
foreach (var postId in postIds) {
var post = _postRepository.GetById(postIds);
_postRepository.Remove(post);
}
// Done with posts
_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);
}
希望这会奏效,或者让您找到可行的答案。基本上,这听起来好像有一个外键约束让您感到困惑。