dotnet / dotnet/efcore

Allow defining keys and indexes across owned types

Open
#38,276 1 comment 3 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-model-building area-owned-entities
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Split from https://github.com/dotnet/efcore/issues/11336

We recently merged several PRs to enable keys and indexes over complex properties. This issue tracks the remaining work, design follow-ups, and any gaps around fully supporting keys and indexes across owned types.

## Goal

Allow defining:

- primary keys
- alternate keys
- indexes

using property paths that traverse owned type boundaries.

## Motivation

A common modeling scenario is to treat values inside owned types as part of an entity's identity or as part of an index. Today this has historically been limited or unsupported in various places, especially when configuration needs to cross owned types boundaries.

We want developers to be able to model scenarios such as:

- an index over `Customer.Name.LastName`
- a unique constraint spanning `TenantId` and `Address.ZipCode`
- keys that include properties declared inside owned types
- consistent behavior for nested owned types

## Scope

This tracking issue covers support for defining keys and indexes across owned types mapped to the same table, including:

- model building APIs
- metadata representation
- migrations

## Example

```csharp
modelBuilder.Entity()
.HasIndex(e => new { e.TenantId, e.Address.ZipCode });

modelBuilder.Entity()
.HasAlternateKey(e => new { e.Name.First, e.Name.Last });

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.