英文:
Entity Framework Core; how to use a many-to-many table's composite key as a foreign key?
问题
The schema below allows a player
to join a profile
using the many-to-many table profile-player
.
该架构允许一个player
通过多对多表profile-player
加入一个profile
。
The table profile-player
contains a composite key for player
and profile
using their associated primary keys. This allows players to be part of 0 or more profiles and a profile to contain 0 or more players.
profile-player
表包含了一个由player
和profile
的相关主键构成的复合键。这允许玩家成为0个或多个profile的一部分,同时一个profile可以包含0个或多个玩家。
This works fine, but when normalizing the data and expanding it into something like profile-player-inventory
, it's using the primary keys of the profile
and player
table when I want it to use the composite keys from the profile-player
table. I need it to use the composite key from the profile-player
as it will cascade delete other rows such as in profile-player-inventory
when the relationship is broken.
这个方案运行良好,但当将数据规范化并扩展为类似profile-player-inventory
的东西时,它使用了profile
和player
表的主键,而我希望它使用profile-player
表的复合键。我需要它使用profile-player
表的复合键,因为当关系破裂时,它将级联删除其他行,例如在profile-player-inventory
中。
I've tried using the FluentAPI that EF Core provides to set its relationship, but I don't see any setting to physically change it to use the profile-player
table's foreign key. It only allows me to set it to the profile
or player
table as shown below.
我尝试使用EF Core提供的FluentAPI来设置它们的关系,但我没有看到任何可以物理更改它以使用profile-player
表的外键的设置。它只允许我将其设置为profile
或player
表,如下所示。
英文:
This is my first question on the website, so if something seems off or the syntax isn't quite right, please let me know!
The schema below allows a player
to join a profile
using the many-to-many table profile-player
.
The table profile-player
contains a composite key for player
and profile
using their associated primary keys. This allows players to be part of 0 or more profiles and a profile to contain 0 or more players.
This works fine, but when normalizing the data and expanding it into something like profile-player-inventory
, it's using the primary keys of the profile
and player
table when I want it to use the composite keys from the profile-player
table. I need it to use the composite key from the profile-player
as it will cascade delete other rows such as in profile-player-inventory
when the relationship is broken.
I've tried using the FluentAPI that EF Core provides to set its relationship, but I don't see any setting to physically change it to use the profile-player
table's foreign key. It only allows me to set it to the profile
or player
table as shown below.
public sealed class ProfilePlayerInventoryConfiguration : IEntityTypeConfiguration<ProfilePlayerInventoryEntity>
{
public void Configure(EntityTypeBuilder<ProfilePlayerInventoryEntity> builder)
{
builder.ToTable("profile_player_inventory");
builder.HasKey(x => new { x.PlayerId, x.ProfileId });
builder.HasOne(x => x.Profile).WithMany(x => x.ProfilePlayerInventorySet);
builder.HasOne(x => x.Player).WithMany(x => x.ProfilePlayerInventorySet);
}
}
答案1
得分: 1
最后,修复方法相当简单。
我决定将我的profile-player
表更改为自动递增的主键,而player-id
和profile-id
则成为唯一索引。
profile-player-inventory
现在如我所期望地引用了profile-player
,如附图所示。
英文:
In the end, the fix was pretty straight forward.
I decided to change my profile-player
table to be an auto-incrementing key with the player-id
and profile-id
being unique indexes instead.
The profile-player-inventory
now references the profile-player
as I wanted it to as in the picture attached.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论