Support complex properties on owned types in JSON-mapped owned collections

Open
#39,692 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp

Research direction

Start by reading the discussion on PR #39522, which this issue says must merge first, then trace EF Core's model validation for complex properties mapped inside JSON owned collections. Check the tests for JSON-mapped owned collections and nested owned types. Done means complex properties work for the described nesting patterns and the model validation error no longer blocks them.

Written by the indexing model from the issue text.

Description

area-complex-types area-owned-entities

Follow up to #39522. Filing this issue now to include the id as a comment, not relevant until that PR is merged.

public class Order
{
    public int Id { get; set; }
    public List<OrderLine> Lines { get; set; } = [];
}

public class OrderLine
{
    public string Sku { get; set; }
    public Money Price { get; set; }
}

public class Money
{
    public decimal Amount { get; set; }
    public string Currency { get; set; }
}
modelBuilder.Entity<Order>().OwnsMany(
    o => o.Lines, b =>
    {
        b.ToJson();
        b.ComplexProperty(l => l.Price);
    });

Expected: Price is stored in each element of the Lines JSON array, as it is for OwnsOne(...).ToJson().

Actual: model validation throws:

Complex property 'OrderLine.Price' cannot be declared on 'OrderLine', which is mapped to JSON as part of an owned collection. Complex properties on owned types in JSON collections aren't supported; map the collection with 'ComplexCollection(...).ToJson()' instead.

The same applies to any owned type below a JSON owned collection: an OwnsOne nested in OwnsMany(...).ToJson(), and an OwnsMany nested in OwnsOne(...).ToJson().

Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.