从会话中删除子记录时出现问题。以下是我定义的实体:
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 :(得分:0)
问题解决了!我只需要先删除报告的项目。看起来有点向后但它有效,这就是我所关心的。