简体   繁体   English

与父母/孩子一起删除问题

[英]Problem Deleting with Parent/Child

I have an issue when deleting a child record from the session. 从会话中删除子记录时出现问题。 Here are the entities i have defined: 这是我定义的实体:

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

With the following fluent mapping: 通过以下流利的映射:

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

Now say i try the following (i have added comments to help you understand what's going on): 现在说我尝试以下方法(我添加了评论以帮助您了解发生了什么事情):

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

When the user trys to delete a forum post (the Action against the reported data is set to delete) it throws the following error: 当用户尝试删除论坛帖子时(针对报告数据的操作设置为删除),它将引发以下错误:

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [ForumPostReported#2] 该行已由另一个事务更新或删除(或未保存的值映射不正确):[ForumPostReported#2]

I could probably set some mapping up to automatically delete the post/comment once the reported data is deleted but i only want to delete the post/comment if the action is set to delete. 我可能会设置一些映射,以便在删除报告的数据后自动删除帖子/评论,但是如果操作设置为删除,我只想删除帖子/评论。

I'd appreciate it if someone could help. 如果有人可以提供帮助,我将不胜感激。 Thanks 谢谢

Problem solved! 问题解决了! I just had to delete the reported item first. 我只需要先删除报告的项目即可。 Seems kinda backwards but it works and that's all i care. 似乎有点倒退,但是它起作用了,这就是我所关心的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM