score:2

Accepted answer

The problem is the composite PK mappings.

For Meetings:

entity.HasKey(x => x.courseId);
entity.HasKey(x => x.meetingDate);

and also for Races:

entity.HasKey(x => x.meetingDate);
entity.HasKey(x => x.courseId);
entity.HasKey(x => x.raceNumber);

There could be similar issue with other entities not shown here.

Looks like you expect that HasKey is additive. But in fact it is overriding, so every HasKey overrides the previous setting and fully redefines the PK to be the one specified for the call.

As shown in Keys (primary) documentation, the composite keys can only be defined fluently using anonymous type syntax new { x.Prop1, x.Prop2, … }. The same applies for any multiple property fluent mapping like alternate key, foreign key, index etc.

The correct PK configuration of the sample entities is respectively

entity.HasKey(x => new { x.courseId, x.meetingDate });

and

entity.HasKey(x => new { x.meetingDate, x.courseId, x.raceNumber });

Related Articles