【发布时间】: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