Support IDictionary navigation property for one-to-many relationship

Open
#21,262 3 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
databases

Research direction

Start by reproducing the model-building exception with the Parent.Children Dictionary and compare it with the ICollection navigation behavior described in the issue. Investigate EF Core's navigation and relationship metadata, then verify that dictionary keys, migrations, querying, and change tracking behave as intended.

Written by the indexing model from the issue text.

Description

area-change-tracking area-o/c-mapping customer-reported

My model uses field Children being (subtype of) IDictionary<child_key, Child> to express one-to-many relationship Parent-Child and supporting easy/fast access to children items.
I would like EF Core to support this pattern in similar way ICollection<Child> Children is handled.

Consider

  class Child {
    public long id { get; set; }
    public string Text { get; set; }
  }
  class Parent {
    public long id { get; set; }
    public string name { get; set; }
    public System.Collections.Generic.Dictionary<long, Child> Children { get; set; }
  }

this throws exception

Unhandled exception. System.InvalidOperationException: The property 'Parent.Children' is of type 'Dictionary<long, Child>' which is not supported by current database provid
er. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

and adding conversion specification like this:

      modelBuilder.Entity<Parent>()
       .Property(m => m.Children)
       .HasConversion(v => v.Values, v => v.ToDictionary(f => f.id, f => f));

doesn't help either.

I also found this question asking for similar feature with a number of hack solutions (https://stackoverflow.com/questions/60726966/ef-core-use-a-dictionary-property), none of them is really addressing the problem though.

When the property is (sub)type of ICollection<Child>, then it's treated as navigational property, with migrations it creates ParentId property in Child table, etc. With IDictionary and appropriate information about keys it could work in similar way and even keep better performance for some tracking operations.

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

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.