"Operation is not valid due to the current state of the object" in some queries with navigation and paging
I have simple Forum application that each topic has many post, and each post may liked by several users. application Entity Models are as following:
public class ForumTopic
{
public int ForumTopicId { get; set; }
public string Subject { get; set; }
public string UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<ForumPost> ForumPosts { get; set; }
}
public class ForumPost
{
public int ForumPostId { get; set; }
public string Message { get; set; }
public int ForumTopicId { get; set; }
public virtual ForumTopic ForumTopic { get; set; }
public string UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<ForumPostFavorite> ForumPostFavorites { get; set; }
}
public class ForumPostFavorite
{
public string UserId { get; set; }
public User User { get; set; }
public int ForumPostId { get; set; }
public ForumPost ForumPost { get; set; }
}
I want take current user favorite posts as following. but this query throws "Operation is not valid due to the current state of the object" Exception. I am sure that this query run in entity framework 6 without exception, but in Entity Framework core this exception occurs.
public IActionResult Favorite()
{
var userId = _userManager.GetUserId(User);
var list = _dbContext.ForumPostFavorites
.Where(e => e.UserId == userId)
.Select(e => e.ForumPost)
.Take(10)
.Select(e => new ForumPostFavoriteVm
{
ForumPostId = e.ForumPostId,
ForumTopicId = e.ForumTopicId,
Message = e.Message,
Subject = e.ForumTopic.Subject,
}).ToList();
return View(list);
}
When I remove this line "Subject = e.ForumTopic.Subject" from query, the query runs without any exceptions.
this is a legitimate bug around flattening - we are trying to flatten query that we are not supposed to.
I have a similar situation. If I remove PostId in Blog class I'll get
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'ConsoleApplication.Blog'
in queue2. Then remove Take(2) and it works again.
` public class Post { public int Id { get; set; } public string Text { get; set; } } public class Blog { public int Id { get; set; } public string Header { get; set; } public int PostId { get; set; } public Post Post { get; set; } }
public class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
}
public class Program
{
public static void Main(string[] args)
{
var options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase()
.Options;
using (var context = new MyContext(options))
{
for (int i = 0; i < 3; i++)
{
context.Posts.Add(new Post { Text = $"Post #{i}"});
}
context.SaveChanges();
var posts = context.Posts.ToList();
for (int i = 0; i < 10; i++)
{
context.Blogs.Add(new Blog{ Post = posts[i % 3]});
}
context.SaveChanges();
}
using (var context = new MyContext(options))
{
var query1 = context.Blogs
.Include(c => c.Post)
.Take(2);
Console.WriteLine("query1");
foreach(var item in query1)
{
Console.WriteLine("blog id: {0} Post: {1} post Id: {2}", item.Id, item.Post.Text, item.Post.Id);
}
var query2 = context.Blogs
.Take(2)
.Select(b => new
{
b.Id,
Text = b.Post.Text
});
Console.WriteLine("query2");
foreach(var item in query2)
{
Console.WriteLine("blog id: {0} Post: {1}", item.Id, item.Text);
}
}
}
}`
Fixed in 667fd2a12ed52a712a7042bb540392e1abbc0818
Is there a workaround available for now?