为什么SelectMany无法用于EF Core Cosmos DB?

时间:2020-09-25 09:25:05

标签: c# .net-core entity-framework-core azure-cosmosdb

对于Entity Framework Core Cosmos提供程序,我想查询类似

SELECT VALUE child
FROM child IN Families.Children
WHERE Families.FamilyName = @familyName

但是下面执行查询会引发异常:

var result = _context.Families.Where(x => x.FamilyName == familyName).SelectMany(f => f.Children).ToListAsync(ct);
InvalidOperationException: The LINQ expression 'DbSet<Family>
.Where(f => f.FamilyName == __familyName_0)
.SelectMany(
source: f => EF.Property<IList<Child>>(f, "Children")
.AsQueryable(),
collectionSelector: (f, c) => new TransparentIdentifier<Family, Child>(
Outer = f,
Inner = c
))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
...

为什么表达式会被翻译,所以它显然有错误的collecion选择器(c,c)

编辑:它没有错误的集合选择器。似乎翻译表达式具有以实体的第一个字母命名的参数,当将实体命名为CapybaraFamily和CapybaraChild时,该参数仍可能引发其他错误。

但是查询仍然无法翻译。

实体配置中是否缺少某些内容?

public void Configure(EntityTypeBuilder<Family> builder)
{
    builder.ToContainer("Families")
        .HasNoDiscriminator()
        .HasIndex(x => x.Id)

    builder.OwnsMany(x => x.Children);
        
    builder.Property(x => x.Id).ToJsonProperty("id");
}

实体:

public class Family : Microsoft.Azure.Documents.Document
{
    public IList<Child> Children { get; set; }
    public string FamilyName { get; set; }
}

public class Child
{
    public string ChildName { get; set; }
    public Guid Id { get; set; }
}

使用Microsoft.EntityFrameworkCore,版本= 3.1.8.0

0 个答案:

没有答案