Error: A second operation started on this context before a previous operation completed

 Author: Shengtao Zhou       Created: 1/10/2019 3:13:48 AM       Modified: 2/14/2019 5:06:36 AM   More...

I got the following error for my MVC Core code Linq query, error:
'A second operation started on this context before a previous operation completed'

This error is usually raised to prevent multiple async queries to run with the same context instance. Since DBContext is not thread safe by nature, the error is very useful to prevent mistakes. By default, DBContext scope is per request, this means you can't run multiple Async queries in the same request.


But for me I only have one query like the following, it does not fall into the usual case.

Linq:

            var comments = (from c in _context.PostComment
                        join u in _context.Users
                            on c.UserId equals u.Id
                        where c.PostId == id
                        select new PostComment
                        {
                            Id = c.Id,
                            Content = c.Content,
                            UserName = u.UserName,
                            UserId = c.UserId,
                            CreateTime = c.CreateTime
                        });
             return View(await comments.ToListAsync< PostComment>());

The error was thrown when calling ToListAsync. It seems the error message is not making any sense. It probably only means there're some syntax I'm using are not supported by entity framework.


I eventually figure out the issue,  the problem is in the PostComment class, "PostId" is not mapped to the database table:

    public class PostComment
    {
        [Key]
        public int Id { get; set; }
        [Required(ErrorMessage = "Please enter contents for your post.")]
        [StringLength(2000, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 100)]
        [DataType(DataType.MultilineText)]
        public string Content { get; set; }
        [StringLength(450)]
        public string UserId { get; set; }
        [NotMapped]
        public string UserName { get; set; }
        public DateTime CreateTime { get; set; }
        [NotMapped]
        public int PostId { get; set;}
        public virtual Post Post { get; set; }
    }

The issue is resolved. The [NotMapped] added to PostId by mistake.  The following 2 lines are defining foreign key PostId, which shouldn't have [NotMapped] . 


        public int PostId { get; set;}
        public virtual Post Post { get; set; }

After I removed [NotMapped], then called add-migration and update-database, the issue was resolved.


PostId is actually a shadow state property. Since PostComment object is referencing Post object, PostId will be added automatically or you can explicitly add it. But if a shadow property is adding with the [NotMapped] attribute the code will break.


Shadow property is one of the new features in EF Core, you can reference this link for detail,

Differences of Entity Framework 6 and Entity Framework Core with Examples


More...          Back to List          

(Please enter you comments between 100 to 2000 characters. Thanks for your contribution.) 

         Created:       Modified: 

Editing a comment

       (Please enter you comments between 100 to 2000 characters. Please login before edit comment.)