dotnet / dotnet/EntityFramework.Docs

Use shadow properties for the key and an alternate key: configuration order matters

Open
#3,093 0 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-model-building
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

I was trying to use shadow properties for the key and an alternate key:

```csharp
public class EntityTypeConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("Table");

builder.HasKey("PrimaryKey");
builder.HasAlternateKey("AdditionalKey");

builder
.Property("PrimaryKey") // The name of the shadow property
.HasColumnName("PK")
.ValueGeneratedOnAdd()
.IsRequired();

builder
.Property("AdditionalKey") // The name of the shadow property
.HasColumnName("U")
.IsRequired();
}
}
```

I got the below exception:

```
System.InvalidOperationException: The property 'PrimaryKey' cannot be added to the type 'Entity' because there was no property type specified and there is no corresponding CLR property or field. To add a shadow state property the property type must be specified.
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(Type propertyType, String propertyName, MemberInfo memberInfo, Nullable`1 typeConfigurationSource, Nullable`1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(Type propertyType, String propertyName, Nullable`1 typeConfigurationSource, Nullable`1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.GetOrCreateProperties(IReadOnlyList`1 propertyNames, Nullable`1 configurationSource, IReadOnlyList`1 referencedProperties, Boolean required, Boolean useDefaultType)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 propertyNames, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.HasKey(String[] propertyNames)
```

Declaring the keys after the properties did the trick, thank to the hint in [Relationships - EF Core](https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#shadow-foreign-key) page:

> We recommend explicitly adding the shadow property to the model before using it as a foreign key (as shown below).

Final working code:

```csharp
public class EntityTypeConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("Table");

builder
.Property("PrimaryKey") // The name of the shadow property
.HasColumnName("PK")
.ValueGeneratedOnAdd()
.IsRequired();

builder
.Property("AdditionalKey") // The name of the shadow property
.HasColumnName("U")
.IsRequired();

builder.HasKey("PrimaryKey");
builder.HasAlternateKey("AdditionalKey");
}
}
```

Wouldn't it be worth mentioning that the shadow property to the model *must* be added before using it as primary or alternate key?

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 550a3c36-7f1d-efa7-3ed8-769720168da4
* Version Independent ID: 627c4301-e00d-826e-d545-3a244305dec9
* Content: [Shadow and Indexer Properties - EF Core](https://docs.microsoft.com/en-us/ef/core/modeling/shadow-properties)
* Content Source: [entity-framework/core/modeling/shadow-properties.md](https://github.com/dotnet/EntityFramework.Docs/blob/master/entity-framework/core/modeling/shadow-properties.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @AndriySvyryd
* Microsoft Alias: **avickers**

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.