【问题标题】:Why SelectMany does not work for EF Core Cosmos DB?为什么 SelectMany 不适用于 EF Core Cosmos DB?
【发布时间】:2021-01-11 15:05:33
【问题描述】:

对于 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."

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

EDIT:它没有错误的集合选择器。似乎将表达式转换为具有以实体的第一个字母命名的参数,当实体被命名为 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

【问题讨论】:

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


    【解决方案1】:

    不幸的是,您需要在 SelectMany 之前执行 ToList。 Cosmos 有一个底层的文档结构,因此它不能与普通实体一起在 sql 哈希中进行远程缓冲。

    SelectMany 之前使用ToList 将首先在本地返回整个集合,但SelectMany 可以在本地工作。

    var result = _context.Families
       .Where(x => x.FamilyName == familyName)
       .ToList()
       .SelectMany(f => f.Children);
    

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2019-05-04
      相关资源
      最近更新 更多