Skip to content

Commit 451bb32

Browse files
author
Bart Koelman
committed
Verified existing one-to-one relationships in our codebase and updated to use an explicit foreign key accordingly. It turns out that not in all cases this makes a difference. I wonder under which circumstances EF Core chooses to use an identifying foreign key and when it does not.
1 parent f044574 commit 451bb32

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

test/JsonApiDotNetCoreExampleTests/IntegrationTests/AtomicOperations/OperationsDbContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3333
builder.Entity<MusicTrack>()
3434
.HasOne(musicTrack => musicTrack.Lyric)
3535
.WithOne(lyric => lyric.Track)
36-
.HasForeignKey<MusicTrack>();
36+
.HasForeignKey<MusicTrack>("LyricId");
3737
}
3838
}
3939
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/EagerLoading/EagerLoadingDbContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ protected override void OnModelCreating(ModelBuilder builder)
2222
builder.Entity<Building>()
2323
.HasOne(building => building.PrimaryDoor)
2424
.WithOne()
25-
.HasForeignKey<Building>("PrimaryDoorKey")
25+
.HasForeignKey<Building>("PrimaryDoorId")
2626
.IsRequired();
2727

2828
builder.Entity<Building>()
2929
.HasOne(building => building.SecondaryDoor)
3030
.WithOne()
31-
.HasForeignKey<Building>("SecondaryDoorKey")
31+
.HasForeignKey<Building>("SecondaryDoorId")
3232
.IsRequired(false);
3333
}
3434
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Links/LinksDbContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override void OnModelCreating(ModelBuilder builder)
2222
builder.Entity<Photo>()
2323
.HasOne(photo => photo.Location)
2424
.WithOne(location => location.Photo)
25-
.HasForeignKey<Photo>("PhotoLocationKey");
25+
.HasForeignKey<Photo>("LocationId");
2626
}
2727
}
2828
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ReadWrite/ReadWriteDbContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3333
builder.Entity<WorkItemGroup>()
3434
.HasOne(workItemGroup => workItemGroup.Color)
3535
.WithOne(color => color.Group)
36-
.HasForeignKey<RgbColor>();
36+
.HasForeignKey<RgbColor>("GroupId");
3737

3838
builder.Entity<WorkItemTag>()
3939
.HasKey(workItemTag => new

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceHooks/HooksDbContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override void OnModelCreating(ModelBuilder builder)
3737
builder.Entity<Passport>()
3838
.HasOne(passport => passport.Person)
3939
.WithOne(person => person.Passport)
40-
.HasForeignKey<Person>("PassportKey")
40+
.HasForeignKey<Person>("PassportId")
4141
.OnDelete(DeleteBehavior.SetNull);
4242
}
4343
}

test/UnitTests/ResourceHooks/HooksDbContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ protected override void OnModelCreating(ModelBuilder builder)
5454
builder.Entity<Passport>()
5555
.HasOne(passport => passport.Person)
5656
.WithOne(person => person.Passport)
57-
.HasForeignKey<Person>("PassportKey")
57+
.HasForeignKey<Person>("PassportId")
5858
.OnDelete(DeleteBehavior.SetNull);
5959

6060
builder.Entity<TodoItem>()
6161
.HasOne(todoItem => todoItem.OneToOnePerson)
6262
.WithOne(person => person.OneToOneTodoItem)
63-
.HasForeignKey<TodoItem>("OneToOnePersonKey");
63+
.HasForeignKey<TodoItem>("OneToOnePersonId");
6464
}
6565
}
6666
}

0 commit comments

Comments
 (0)