Cosmos Where query on owned properties throws a translation exception
See original GitHub issueAsk a question
I’m in the process of implementing a Cosmos DB access with an entity framework provider, And every query I’m trying to run on Owned Entity gets a translation error. I tried many different queries. and I get this error only on owned entities like in the code example. I think this query type should be possible like in this test: https://github.com/dotnet/efcore/blob/main/test/EFCore.Cosmos.FunctionalTests/Query/OwnedQueryCosmosTest.cs
code
The (simplefied) document classes
public class StructuredTrial
{
public string TrialId { get; set; }
public ICollection<StringWrapper> ConditionCodesList { get; set; }
}
public class StringWrapper
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
public string StrValue { get; set; }
}
Context definition:
modelBuilder.HasDefaultContainer("StructuredTrials");
modelBuilder.Entity<StructuredTrial>().HasKey(s => s.TrialId);
modelBuilder.Entity<StructuredTrial>().HasPartitionKey(s => s.TrialId);
modelBuilder.Entity<StructuredTrial>().OwnsMany(c => c.ConditionCodesList, c =>
{
c.HasKey(p => p.Id);
});
Calling code I tried, with similar error (error provided is for the first one):
var db = new KnowledgeGraphDBContext(_dbOptions);
var studiesQuery = await db.StructuredTrials.AsQueryable()
.Where(s => s.ConditionCodesList.IsDefined()).ToListAsync();
var studiesQuery = await db.StructuredTrials.AsQueryable()
.Where(s => s.ConditionCodesList.Count() > 0).ToListAsync();
var studiesQuery = await db.StructuredTrials.AsQueryable()
.Where(s => s.ConditionCodesList.Contains(null)).ToListAsync();
Error Output
System.InvalidOperationException: 'The LINQ expression 'DbSet<StructuredTrialTest>()
.Where(s => MaterializeCollectionNavigation(
Navigation: StructuredTrialTest.ConditionCodesList,
subquery: EF.Property<ICollection<StringWrapperTest>>(s, "ConditionCodesList")
.AsQueryable()
.IsDefined())' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
stack traces
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& ) in /_/src/EFCore/Query/QueryableMethodTranslatingExpressionVisitor.cs:line 105
Include provider and version information
EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.Cosmos 5.0.12) Target framework: (e.g. .NET 3.1) Operating system: IDE: (e.g. Visual Studio 2022 17.0.1)
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Cosmos: Translate subquery in filter condition · Issue #17957
However, this results in a query translation error: System. ... Cosmos Where query on owned properties throws a translation exception #26849.
Read more >LINQ to NoSQL translation - Azure Cosmos DB
If you want to get the NoSQL query that is translated from LINQ, use the ToString() method on the generated IQueryable object. The...
Read more >Breaking changes in EF Core 6.0
Changing the owner of an owned entity now throws an exception, Medium ; Azure Cosmos DB: Related entity types are discovered as owned,...
Read more >create/query in cosmos throws cancelled exception
In my logs I see the number of operations is enormous of zero ru cost. My app insights dependencies failures is crazy.
Read more >Access Azure Cosmos DB with Entity Framework Core
Entity Framework (EF) Core is the data access API of choice for .NET developers. Although considered by most to be an object relational ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@yochail Which test do you mean, specifically? Many of those tests are skipped with reference to #17246.
@smitpatel
IsDefined
comes from theMicrosoft.Azure.Cosmos.Linq
LINQ provider. It’s interesting to consider that people may try to use these with the EF Core LINQ provider./cc @AndriySvyryd