我想将 table ID 用于不同的 table,它需要两个 tables ID,Entity Framework,代码优先
I want to use a table ID to a different table that takes two of that tables ID, Entity Framework, code first
这只是我快速完成的事情,但它显示了我想要的样子。 OBS数据库图table图片。所以我想让Team
table和Transfer
table有两个关系(OldTeamId
,NewTeamId
),这样可以吗?
当我尝试按现在的样子执行 Add-Migration
时,我收到一条消息
Unable to determine the relationship represented by navigation property 'Team.OldTeams' of type 'List'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
代码:
public class Transfer
{
public int Id { get; set; }
public DateTime TransferDate { get; set; }
public Player ThePlayer { get; set; }
public string PlayerId { get; set; }
public Team OldTeam { get; set; }
public int OldTeamId { get; set; }
public Team NewTeam { get; set; }
public int NewTeamId { get; set; }
}
public class Team
{
public Team()
{
Tournaments = new List<Tournament>();
Players = new List<TeamPlayer>();
OldTeams = new List<Transfer>();
NewTeams = new List<Transfer>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] TeamLogo { get; set; }
public DateTime Founded { get; set; }
public Country TheCountry { get; set; }
public int CountryId { get; set; }
public List<Tournament> Tournaments { get; set; }
public List<TeamPlayer> Players { get; set; }
public List<Transfer> OldTeams { get; set; }
public List<Transfer> NewTeams { get; set; }
}
public class GamepediaContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Map> Maps { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<TeamPlayer> TeamPlayers { get; set; }
public DbSet<Tournament> Tournaments { get; set; }
public DbSet<TournamentMap> TournamentMaps { get; set; }
public DbSet<Transfer> Transfers { get; set; }
}
[1]: https://i.stack.imgur.com/Y8Set.png
使用 foreignKey 属性
public class Transfer
{
public int Id { get; set; }
public DateTime TransferDate { get; set; }
public Player ThePlayer { get; set; }
public string PlayerId { get; set; }
public Team OldTeam { get; set; }
[ForeignKey("OldTeam")]
public int OldTeamId { get; set; }
public Team NewTeam { get; set; }
[ForeignKey("NewTeam")]
public int NewTeamId { get; set; }
}
我让它工作了,我添加了这行代码。我将脚本 "onDelete" 从 Cascade 更改为 NoAction。
modelBuilder.Entity<Team>().HasMany(m => m.OldTeams).WithOne(m => m.OldTeam).HasForeignKey(m => m.OldTeamId);
modelBuilder.Entity<Team>().HasMany(m => m.NewTeams).WithOne(m => m.NewTeam).HasForeignKey(m => m.NewTeamId);
这只是我快速完成的事情,但它显示了我想要的样子。 OBS数据库图table图片。所以我想让Team
table和Transfer
table有两个关系(OldTeamId
,NewTeamId
),这样可以吗?
当我尝试按现在的样子执行 Add-Migration
时,我收到一条消息
Unable to determine the relationship represented by navigation property 'Team.OldTeams' of type 'List'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
代码:
public class Transfer
{
public int Id { get; set; }
public DateTime TransferDate { get; set; }
public Player ThePlayer { get; set; }
public string PlayerId { get; set; }
public Team OldTeam { get; set; }
public int OldTeamId { get; set; }
public Team NewTeam { get; set; }
public int NewTeamId { get; set; }
}
public class Team
{
public Team()
{
Tournaments = new List<Tournament>();
Players = new List<TeamPlayer>();
OldTeams = new List<Transfer>();
NewTeams = new List<Transfer>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] TeamLogo { get; set; }
public DateTime Founded { get; set; }
public Country TheCountry { get; set; }
public int CountryId { get; set; }
public List<Tournament> Tournaments { get; set; }
public List<TeamPlayer> Players { get; set; }
public List<Transfer> OldTeams { get; set; }
public List<Transfer> NewTeams { get; set; }
}
public class GamepediaContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Map> Maps { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<TeamPlayer> TeamPlayers { get; set; }
public DbSet<Tournament> Tournaments { get; set; }
public DbSet<TournamentMap> TournamentMaps { get; set; }
public DbSet<Transfer> Transfers { get; set; }
}
[1]: https://i.stack.imgur.com/Y8Set.png
使用 foreignKey 属性
public class Transfer
{
public int Id { get; set; }
public DateTime TransferDate { get; set; }
public Player ThePlayer { get; set; }
public string PlayerId { get; set; }
public Team OldTeam { get; set; }
[ForeignKey("OldTeam")]
public int OldTeamId { get; set; }
public Team NewTeam { get; set; }
[ForeignKey("NewTeam")]
public int NewTeamId { get; set; }
}
我让它工作了,我添加了这行代码。我将脚本 "onDelete" 从 Cascade 更改为 NoAction。
modelBuilder.Entity<Team>().HasMany(m => m.OldTeams).WithOne(m => m.OldTeam).HasForeignKey(m => m.OldTeamId);
modelBuilder.Entity<Team>().HasMany(m => m.NewTeams).WithOne(m => m.NewTeam).HasForeignKey(m => m.NewTeamId);