dotnet / dotnet/efcore

Data denormalization in Ef Core and Azure Cosmos Db

Open
#33,727 3 comments 1 reaction 0 assignees View on GitHub
area-cosmos area-model-building customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

How do I denormalize data in Ef Core with Azure cosmos db provider?

As shown in this documentation: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/model-partition-example#v2-introducing-denormalization-to-optimize-read-queries

I need to denormalize an entity which also lives in another container:

```C#
public class Post
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public List Hashtags { get; set; } = new List();
}

public class Comment
{
public int Id { get; set; }
public int PostId { get; set; }
public string Text { get; set; } = null!;
public List Hashtags { get; set; } = new List();
}
public class Hashtag
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public int Puntuation { get; set; }
}
```
So for faster queries I need to embed the Hashtag in the Comment and in the Post (this is just an example, not a real use case), but the hashtag lives in it's own container.

I know this it's not possible in Ef Core due to I'll se an error like this: 'The type 'Hashtag' cannot be marked as owned because a non-owned entity type with the same name already exists.'

Any solution?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.