Add support for collections of primitive types as separate table in relational databases

Open
#25,163 6 comments 48 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by tracing the relational mapping path used by OwnsMany and how primitive collections are currently represented or rejected. Compare the requested Role.Members configuration with existing owned-collection behavior and verify how querying, foreign keys, keys, and schema creation would work. Done means a configured primitive collection can be stored in its own relational table and queried by its values.

Written by the indexing model from the issue text.

Description

area-model-building area-primitive-collections customer-reported

Currently it's not possible to store collections of primitive types in their own table so that we can easily query on those values. All proposed solutions are to serialize the collection but this does not allow for querying it's content easily or to create a class with a single property to represent the new table.

Through configuration it would be great to be able to define a table assigned to the collection so that it can be stored in it's own table with a foreign key referencing the parent entity. This could be configured like a collection of owned entities stored in it's own table where the value of the collection is the only column (with the foreign key).

So the entity:

public class Role
{
  public string Id { get; set; }
  public string Name { get; set; }
  public ICollection<string> Members { get; set; }
}

with the following configuration:

modelBuilder.Entity<Role>().OwnsMany(
    r => r.Members, 
    b => {
        b.ToTable("RoleMembers");
        b.WithOwner().HasForeignKey("RoleId");
        b.Property(b => b).HasColumnName("Name");
        b.HasKey("RoleId", "Name");
    });

would be persisted in 2 tables:

  • Roles
    • Id
    • Name
  • RoleMembers
    • RoleId
    • Name
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.