簡體   English   中英

與父母/孩子一起刪除問題

[英]Problem Deleting with Parent/Child

從會話中刪除子記錄時出現問題。 這是我定義的實體:

public class ReportedData
{
    public virtual int ReportedDataID { get; set; }
    public virtual ReportedDataTypes Type { get; set; }
    public virtual string Reason { get; set; }

    public virtual IList<ArticleCommentReported> ArticleCommentsReported { get; private set; }
    public virtual IList<ForumPostReported> ForumPostsReported { get; private set; }

    public ReportedData()
    {
        ArticleCommentsReported = new List<ArticleCommentReported>();
        ForumPostsReported = new List<ForumPostReported>();
    }
}

public class ArticleCommentReported : ReportedData
{
    public virtual ArticleComment Comment { get; set; }
}

public class ForumPostReported : ReportedData
{
    public virtual ForumPost Post { get; set; }
}

通過以下流利的映射:

public ReportedDataMap()
{
    Table("ReportedData");
    Id(x => x.ReportedDataID);
    Map(x => x.Type, "TypeID");
    Map(x => x.Reason);
    HasMany(x => x.ArticleCommentsReported)
        .KeyColumn("ReportedDataID")
        .Inverse()
        .Cascade.All();
    HasMany(x => x.ForumPostsReported)
        .KeyColumn("ReportedDataID")
        .Inverse()
        .Cascade.All();
}

public class ArticleCommentReportedMap : SubclassMap<ArticleCommentReported>
{
    public ArticleCommentReportedMap()
    {
        Table("ArticleCommentsReported");
        KeyColumn("ReportedDataID");
        References(x => x.Comment, "CommentID");
    }
}

public class ForumPostReportedMap : SubclassMap<ForumPostReported>
{
    public ForumPostReportedMap()
    {
        Table("ForumPostsReported");
        KeyColumn("ReportedDataID");
        References(x => x.Post, "PostID");
    }
}

現在說我嘗試以下方法(我添加了評論以幫助您了解發生了什么事情):

// Loop over the reported data (this is my view model and not my actual model which contains an extra property for the action they wish to carry out)
foreach (var reportedData in model)
{
    // If the action is leave then do nothing (else we always delete the reported data)
    if (reportedData.Action != ReportedDataActions.Leave)
    {
        // Switch over the type since we need to make sure it deletes the article comment or post if the action is set to delete
        switch (reportedData.Type)
        {
            case ReportedDataTypes.ArticleComment:
                var reportedComment = _context.Repository<ArticleCommentReported>().GetByID(reportedData.ReportedDataID);

                if (reportedData.Action == ReportedDataActions.Delete)
                    _context.Repository<ArticleComment>().Delete(reportedComment.Comment);

                _context.Repository<ArticleCommentReported>().Delete(reportedComment);

                break;
            case ReportedDataTypes.ForumPost:
                var reportedPost = _context.Repository<ForumPostReported>().GetByID(reportedData.ReportedDataID);

                if (reportedData.Action == ReportedDataActions.Delete)
                    _forumService.DeletePost(reportedPost.Post);

                _context.Repository<ForumPostReported>().Delete(reportedPost);

                break;
        }
    }
}

_context.Commit();

當用戶嘗試刪除論壇帖子時(針對報告數據的操作設置為刪除),它將引發以下錯誤:

該行已由另一個事務更新或刪除(或未保存的值映射不正確):[ForumPostReported#2]

我可能會設置一些映射,以便在刪除報告的數據后自動刪除帖子/評論,但是如果操作設置為刪除,我只想刪除帖子/評論。

如果有人可以提供幫助,我將不勝感激。 謝謝

問題解決了! 我只需要先刪除報告的項目即可。 似乎有點倒退,但是它起作用了,這就是我所關心的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM