gpt4 book ai didi

c# - Entity Framework - 多对多?

转载 作者:行者123 更新时间:2023-11-29 08:26:10 24 4
gpt4 key购买 nike

我定义多对多关系如下:

    modelBuilder.Entity<GameSessionEntry>().
HasMany(c => c.Users).
WithMany(p => p.GameSessionEntries).
Map(
m =>
{
m.MapLeftKey("SessionId");
m.MapRightKey("UserId");
m.ToTable("UserSessions");
});

但是,我不断得到:

The Foreign Key on table 'UserSessions' with columns 'UserId' could not be created because the principal key columns could not be determined. Use the AddForeignKey fluent API to fully specify the Foreign Key.

我是数据库工作和 EntityFramework 的新手 - 它要求我做什么?

最佳答案

这是反复出现的左右混淆,请参阅this斯劳玛的解释。所以你只需要把键名调换一下:

  m.MapLeftKey("UserId");      // Property in the HasMany call
m.MapRightKey("SessionId"); // Property in the WithMany call

关于c# - Entity Framework - 多对多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17850471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com