【发布时间】:2021-05-04 04:01:33
【问题描述】:
我从 NBA API 获取数据,为了将数据写入我的数据库,我需要使用复合键 PlayerID 和 GameID 创建 PlayerGame 的 DB 对象,我尝试使用两个 [key][column=] 标签来定义但是在尝试添加迁移时,我得到了错误需要使用流利的 api。所以经过一番谷歌搜索后,我想出了以下代码,现在在尝试迁移时,我收到错误消息实体类型“IdentityUserLogin”需要定义主键。如果您打算使用无密钥实体类型,请调用“HasNoKey()”。我猜它与继承自 IdentityDbContext 的 ApplicationDbCONtext 有关,但我现在真的不知道该怎么做。请帮忙!!
我有一个类玩家游戏
public class PlayerGame
{
[Required]
public int PlayerID { get; set; }
[ForeignKey ("PlayerID")]
public Player Player { get; set; }
[Required]
public int GameID { get; set; }
[ForeignKey ("GameID")]
public Game Game { get; set; }
和数据库上下文
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<Player> Player {get; set; }
public DbSet<Game> Game { get; set; }
public DbSet<PlayerGame> PlayerGame { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PlayerGame>()
.HasKey(pg => new { pg.PlayerID, pg.GameID });
}
【问题讨论】:
标签: c# asp.net-core