You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support IDictionary navigation property for one-to-many relationship #21262
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));
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.
Reacted by Mohammad Omidvar, Markz878, Jan Selchow, Darkwell, Victor Irzak, Andrew Thornton, nader marandi, mgraf1, Joni Aromaa, IT-CASADO and 1 more
@kskalski This is an interesting idea. Issue #752 is tracking custom collection mapping, but this is somewhat more than that because it would require EF to use the dictionary for internal look-ups on the collection,
While modelling similar relationship using existing capabilities I realized some difficulties of using IDictionary - namely the semantics of keys and operations related to adding the hierarchy to EF.
The key of dictionary might be used in two ways:
when it's a (global) primary key of the Child entity, then it seems kind of easy and understandable for users of this data, however there is a bit painful limitation when adding a parent with a set of new children.
[Currently EF supports adding multiple related entities at the same time, e.g. new A entity and several B entities stored in A's ICollection<B> navigational property, EF will take care of assigning new primary keys to both A and all Bs and the (generated by DB) ids are not affecting the structure / navigational properties.]
When primary key of Child is to be generated by DB during addition, it's not clear how to create IDictionary with a bunch of them before they are added to DB, multiple Saves are required to do it properly. When I had such problem in my APIs I used some hacks, like assigning fake ids from invalid key domain (e.g. negative numbers).
For my data model, I would rather use an IDictionary key that is relative to the Parent (so not a global primary key, but local key in scope of that single parent). In that case Child could have a separate global primary key (Id) and also a "relative key" field (e.g. Name) used as key in IDictionary.
This approach solves the problem of adding the whole hierarchy of entities at once, provides a possibly more convenient model of using "scoped key", but I guess it requires a richer API and support from EF to specify foreign keys for IDictionary mapping properly.
Both 1. and 2. also require a ParentId field, but in case of 2. it's optionally possible to drop Child's Id field in favor of composite primary key (ParentId, Name), though it has kind of a "scaling" problem (imagine a deeper hierarchy with GrandChild and GrandGrandChild, they would require addressing all the ancestors' keys in their primary key).
Activity
ajcvickers commentedon Jun 15, 2020
@kskalski This is an interesting idea. Issue #752 is tracking custom collection mapping, but this is somewhat more than that because it would require EF to use the dictionary for internal look-ups on the collection,
kskalski commentedon Jun 19, 2020
While modelling similar relationship using existing capabilities I realized some difficulties of using
IDictionary- namely the semantics of keys and operations related to adding the hierarchy to EF.The key of dictionary might be used in two ways:
when it's a (global) primary key of the
Childentity, then it seems kind of easy and understandable for users of this data, however there is a bit painful limitation when adding a parent with a set of new children.[Currently EF supports adding multiple related entities at the same time, e.g. new A entity and several B entities stored in A's
ICollection<B>navigational property, EF will take care of assigning new primary keys to both A and all Bs and the (generated by DB) ids are not affecting the structure / navigational properties.]When primary key of
Childis to be generated by DB during addition, it's not clear how to create IDictionary with a bunch of them before they are added to DB, multiple Saves are required to do it properly. When I had such problem in my APIs I used some hacks, like assigning fake ids from invalid key domain (e.g. negative numbers).For my data model, I would rather use an
IDictionarykey that is relative to theParent(so not a global primary key, but local key in scope of that single parent). In that case Child could have a separate global primary key (Id) and also a "relative key" field (e.g.Name) used as key in IDictionary.This approach solves the problem of adding the whole hierarchy of entities at once, provides a possibly more convenient model of using "scoped key", but I guess it requires a richer API and support from EF to specify foreign keys for IDictionary mapping properly.
Both 1. and 2. also require a
ParentIdfield, but in case of 2. it's optionally possible to dropChild'sIdfield in favor of composite primary key(ParentId, Name), though it has kind of a "scaling" problem (imagine a deeper hierarchy with GrandChild and GrandGrandChild, they would require addressing all the ancestors' keys in their primary key).AndriySvyryd commentedon Apr 22, 2023
This is a specific case of #2919
Edgaras91 commentedon Oct 15, 2026
Hey, wondering if there is any update/progress or hope that this feature will ever come out?
AndriySvyryd commentedon Oct 15, 2026
@Edgaras91 There are currently no specific plans for this. As long as it's still open it will be considered.