开发者

Problem Deleting with Parent/Child

开发者 https://www.devze.com 2023-01-19 05:00 出处:网络
I have an issue when deleting a child record from the session.Here are the entities i have defined: public class ReportedData

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开发者_如何学C>().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]

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消