How to query and filter data in a collection property that configured to its owner by OwnsMany function?
This is the sample I've tried:
using (var context = new BloggingContext())
{
context.Database.OpenConnection();
context.Database.EnsureCreated();
context.Blogs.Add(sampleBlog);
context.SaveChanges();
var blog = context.Blogs.Single(b => b.BlogId == 1);
var goodPosts = context.Entry(blog)
.Collection(b => b.Posts)
.Query()
.Where(p => p.Title == "...")
.ToList();
}
and Model classes:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
and dbContext class:
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().OwnsMany(x => x.Posts, post =>
{
post.ToTable("Posts");
post.HasKey("Id");
post.Property(x => x.Title);
post.Property(x => x.Content);
});
modelBuilder.Entity<Blog>()
.ToTable("Blogs");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connection = new SqliteConnection("Data Source=:memory:");
optionsBuilder.UseSqlite(connection);
}
}
I'm getting this exception:
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
Inner Exception:
ArgumentException: Expression of type 'System.Collections.Generic.List`1[Post]' cannot be used for return type 'Post'