C# 删除父/子对象时出现问题

C# 删除父/子对象时出现问题,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,从会话中删除子记录时出现问题。以下是我定义的实体: public class ReportedData { public virtual int ReportedDataID { get; set; } public virtual ReportedDataTypes Type { get; set; } public virtual string Reason { get; set; } public virtual IList<ArticleComme

从会话中删除子记录时出现问题。以下是我定义的实体:

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; }
}
公共类报告数据
{
公共虚拟int ReportedDataID{get;set;}
公共虚拟ReportedDataTypes类型{get;set;}
公共虚拟字符串原因{get;set;}
公共虚拟IList ArticleCommentsReported{get;private set;}
公共虚拟IList ForumPostsReported{get;private set;}
公共报告数据()
{
ArticleCommentsReported=新列表();
ForumPostsReported=新列表();
}
}
公共类ArticleCommentReported:ReportedData
{
公共虚拟物品注释{get;set;}
}
umpostreport的公共类:ReportedData
{
公共虚拟ForumPost{get;set;}
}
使用以下fluent映射:

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");
    }
}
public ReportedDataMap()
{
表(“报告数据”);
Id(x=>x.ReportedDataID);
Map(x=>x.Type,“TypeID”);
Map(x=>x.Reason);
HasMany(x=>x.ArticleComments报告)
.KeyColumn(“ReportedDataID”)
.Inverse()
.Cascade.All();
HasMany(x=>x.ForumPostsReported)
.KeyColumn(“ReportedDataID”)
.Inverse()
.Cascade.All();
}
公共类ArticleCommentReportedMap:子类映射
{
public Article CommentReportedMap()
{
表(“文章评论报告”);
KeyColumn(“ReportedDataID”);
参考文献(x=>x.Comment,“CommentID”);
}
}
UMPostReportedMap的公共类:子类映射
{
public ForumPostReportedMap()
{
表(“ForumPostsReported”);
KeyColumn(“ReportedDataID”);
参考文献(x=>x.Post,“posted”);
}
}
现在,假设我尝试以下方法(我添加了一些注释,以帮助您了解发生了什么):

//循环报告的数据(这是我的视图模型,而不是我的实际模型,它包含他们希望执行的操作的额外属性)
foreach(模型中的var报告数据)
{
//如果操作是“离开”,则什么也不做(否则我们总是删除报告的数据)
如果(reportedData.Action!=ReportedDataActions.Leave)
{
//切换类型,因为如果操作设置为delete,我们需要确保它删除文章注释或帖子
开关(reportedData.Type)
{
案例报告数据类型.ArticleComment:
var reportedComment=_context.Repository().GetByID(reportedData.ReportedDataID);
如果(reportedData.Action==ReportedDataActions.Delete)
_context.Repository().Delete(reportedComment.Comment);
_context.Repository().Delete(reportedComment);
打破
案例报告数据类型。ForumPost:
var reportedPost=_context.Repository().GetByID(reportedData.ReportedDataID);
如果(reportedData.Action==ReportedDataActions.Delete)
_forumService.DeletePost(reportedPost.Post);
_context.Repository().Delete(reportedPost);
打破
}
}
}
_Commit();
当用户尝试删除论坛帖子(针对报告数据的操作设置为delete)时,会抛出以下错误:

行被另一个事务更新或删除(或未保存的值映射不正确):[ForumPostReported#2]

我可能会设置一些映射,以便在删除报告的数据后自动删除帖子/评论,但我只想在操作设置为删除时删除帖子/评论


如果有人能帮忙,我将不胜感激。谢谢

问题解决了!我只需要先删除报告的项目。似乎有点落后,但它的工作,这就是我所关心的

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