2010-10-05 75 views
0

从会话中删除子记录时出现问题。这里是我所定义的实体:用父/子删除问题

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]

我大概可以设置一些映射为自动删除该帖子/评论一次报告的数据将被删除但如果该操作设置为删除,我只想删除帖子/评论。

我会很感激,如果有人可以帮忙。谢谢

回答

0

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