Entity Framework Core; 如何使用多对多表的复合键作为外键?

huangapple go评论62阅读模式
英文:

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表包含了一个由playerprofile的相关主键构成的复合键。这允许玩家成为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的东西时,它使用了profileplayer表的主键,而我希望它使用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表的外键的设置。它只允许我将其设置为profileplayer表,如下所示。

英文:

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.

Entity Framework Core; 如何使用多对多表的复合键作为外键?

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-idprofile-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.

Solution

huangapple
  • 本文由 发表于 2023年5月18日 01:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定