question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cosmos: How to configure DbContext for an infinite/recursive owned entity

See original GitHub issue

Hi!

Out of the documentation for Owned entities and cosmos, I cannot see if there is, or should be, a way to configure a DbContext so that an owned entity, that is recursive, will serialise the complete structure of the entity to Cosmos.

Steps to reproduce

  1. Create a containing Entity that ownes the recursive entity.
  2. Configure the DbContext with Owned for the recursive entity.

Since the recursive entity owns entities of itself, there should be a way to configure the context to just follow the navigation properties as long as there is relevant children populated in the entity graph.

This is what we would have:

//Entities
 public class RootEntity
 {
        public string Id { get; set; }
        public string PartitionKey { get; set; }
        public string Name { get; set; }
        
        public IEnumerable<RecursiveEntity> Children { get; set; }
}

public class RecursiveEntity
{
        public string Name { get; set; }
        public IEnumerable<RecursiveEntity> Children { get; set; }
}
//Context
modelBuilder.Owned<RecursiveEntity>();

modelBuilder.Entity<RootEntity>().HasPartitionKey(o => o.PartitionKey);
modelBuilder.Entity<RootEntity>().OwnsMany(p => p.Children);

Ideally in a pure Cosmos scenario this could be enough just to serialise the recursive structure(s) indefinitely since all entities in the RecursiveEntity are owned, but as of now we cannot find a way forward. We need a way to configure the DbContext to understand to navigate the recursive entity graph.

To set at fixed depth by nesting .OwnsMany() in the context is unfortunately not an option in this case for the entities.

Further technical details

EF Core version: 3.1.0-preview3.19554.8 Database provider: Cosmos DB Target framework: .NET Core 3.1 Preview 3 Operating system: Mac OS X, Windows 10 IDE: Visual Studio Code

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Nov 18, 2019

Would there be a way to know in advance if/when there will be changes to the __jObject behaviour?

@gdahlin We’ll mention it in the breaking changes for the version in which it will change. There are currently no plans to change it.

0reactions
ajcvickerscommented, Nov 10, 2021

@aladd04 We just discussed this as a team and it is not something we plan to implement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Owned Entity Types - EF Core
In this article. Configuring types as owned; Implicit keys; Collections of owned types; Mapping owned types with table splitting ...
Read more >
How to do recursive load with Entity framework?
By manually iterating over the result set with a foreach() loop, and adding those root items to a new List<TreeSet>() , you will...
Read more >
Entity Framework Visual Editor
This Visual Studio 2019 extension is the easiest way to add a consistently correct Entity Framework model to your project with support for ......
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 >
Using Azure CosmosDB With .NET Core – Part 2 – EF Core
Setting Up EFCore For Cosmos. I'm going to focus on Cosmos only information here, and not get too bogged down in details around...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found