2010-10-05 4 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(); 

Когда пользователь пытается удалить сообщение в форуме (действие против сообщаемых данных установлено на удаление), он выдает следующую ошибку:

Row был обновлен или удален другой транзакция (или отображение неспасенного значения было неправильным): [ForumPostReported # 2]

Я мог бы установить некоторое отображение для автоматического удаления поста/комментария после того, как сообщили данные удаляются но я хочу удалить сообщение/комментарий, если действие установлено для удаления.

Буду признателен, если кто-то может помочь. Thanks

ответ

0

Проблема решена! Мне просто пришлось сначала удалить сообщенный элемент. Кажется любопытным назад, но это работает, и это все, что мне нужно.

Смежные вопросы