简体   繁体   English

将 SaveChanges() 方法与 EF 核心一起使用时出错

[英]Error in Using SaveChanges() Method with EF core

In the following code, I receive an error in _context.SaveChanges();在以下代码中,我在_context.SaveChanges();中收到错误消息。 when adding a new record to FeedbackComments inside the first foreach loop.在第一个foreach循环内向FeedbackComments添加新记录时。 The error is New transaction is not allowed because there are other threads running in the session.错误是New transaction is not allowed because there are other threads running in the session. . . Any ideas why this is happening?任何想法为什么会发生这种情况?

BTW, I keep receiving the same error when SaveChanges is called only once after the outer loop.顺便说一句,当在外循环之后只调用一次SaveChanges时,我一直收到同样的错误。

List<FeedbackComment> feedbackComments = comments.Select(c => new FeedbackComment
{
    Id = c.Id,
    CommentText = c.Content,
    SubmissionId = submissionId,
    UserDisplayName = c.Author.DisplayName,
    DateCreated = c.CreatedTime.GetValueOrDefault(),
    FeedbackReplies = c.Replies.Select(r => new FeedbackReply
    {
        Id = r.Id,
        UserDisplayName = r.Author.DisplayName,
        ReplyText = r.Content,
        DateCreated = r.CreatedTime.GetValueOrDefault(),
        FeedbackCommentId = c.Id
    }).ToList()
}).ToList();

_context.SaveChanges();


foreach (FeedbackComment c in feedbackComments)
{
    if (!_context.FeedbackComments.Any(fc => fc.Id == c.Id))
    {
        ApplicationUser commentOwner = _context.ApplicationUsers.FirstOrDefault(au => au.GoogleDisplayName == c.UserDisplayName);

        if(commentOwner != null)
        {
            c.UserId = commentOwner.Id;

            _context.FeedbackComments.Add(c);
            newComments = true;
            _context.SaveChanges();

        }

    }


    foreach (FeedbackReply r in c.FeedbackReplies)
    {
        if (!_context.FeedbackReplies.Any(fr => fr.Id == r.Id))
        {
            ApplicationUser replyOwner = _context.ApplicationUsers.FirstOrDefault(au => au.GoogleDisplayName == c.UserDisplayName);

            if (replyOwner != null)
            {
                r.UserId = replyOwner.Id;

                _context.FeedbackReplies.Add(r);
                newComments = true;
                _context.SaveChanges();

            }
        }

    }
}

When you are trying to save a change using a transaction, you should wait until any other transactions are completed.当您尝试使用事务保存更改时,您应该等到任何其他事务完成。 On the other hand, waiting for the previous transaction to be completed, makes severe performance issue.另一方面,等待前一个事务完成会带来严重的性能问题。 You should put _context.SaveChanges();你应该把_context.SaveChanges(); outside the foreach loop like this:foreach循环之外,如下所示:

foreach (FeedbackReply r in c.FeedbackReplies)
{
    if (!_context.FeedbackReplies.Any(fr => fr.Id == r.Id))
    {
        ApplicationUser replyOwner = _context.ApplicationUsers.FirstOrDefault(au => au.GoogleDisplayName == c.UserDisplayName);

        if (replyOwner != null)
        {
            r.UserId = replyOwner.Id;
            _context.FeedbackReplies.Add(r);
            newComments = true;
        }
    }
}
_context.SaveChanges();

In the above code, all changes are applied to the database in one transaction.在上面的代码中,所有更改都在一个事务中应用到数据库。

Why are you calling SaveChanges after the initial data fetch?为什么在初始数据获取后调用 SaveChanges? If you leave that one out and call SaveChanges only once, at the end, it should work, as Hadi said.正如哈迪所说,如果你把那个放在外面,只调用一次 SaveChanges,最后它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM