EF:如何在一个联结表中映射多个FK?

时间:2013-09-05 21:58:57

标签: c# .net entity-framework many-to-many

EF代码优先于4.3.1 这是一个幻想体育工具的简单模式:

Schema

这是一种经典的多对多关系。 Player表具有PlayerId + LeagueId的复合键,因此可以转移到联结表。 (注意:PlayerId在表中并不是唯一的,给定的联盟(比如NFL)可能会使用相同的球员ID作为不同的联赛(例如NBA)

当我尝试像这样定义映射时,我得到EF错误:

this.HasMany(t => t.Players).WithMany(p => p.FantasyTeams).Map(mc => {
            mc.ToTable("FantasyTeam_2_Player");
            mc.MapLeftKey("PlayerId", "LeagueId");
            mc.MapRightKey("FantasyTeamId");
        });

像这样我得到了一个神秘的错误:无效的列名'League_LeagueId'。

如果我切换右和左,我得到:指定的关联外键列'PlayerId'无效。指定的列数必须与主键列的数量匹配。

编辑:这是实体

FantasyTeam

    public int FantasyTeamId { get; set; }
    public string TeamName { get; set; }
    public string TeamEmail { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime LastModified { get; set; }
    public bool Active { get; set; }

    public virtual ICollection<Player> Players { get; set; }

播放器

    public virtual League League { get; set; }
    public int PlayerId { get; set; }
    public int LeagueId { get; set; }
    public string PlayerType { get; set; }
    public string Name { get; set; }
    public string Team { get; set; }
    public string Position { get; set; }

    public virtual ICollection<FantasyTeam> FantasyTeams { get; set; }

映射

public Player_Mapping() {
        this.HasKey(t => new { t.PlayerId, t.LeagueId });
        this.ToTable("Player");
        this.Property(t => t.PlayerId).HasColumnName("PlayerId").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(t => t.LeagueId).HasColumnName("LeagueId").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(t => t.PlayerType).HasColumnName("PlayerType").HasMaxLength(50).IsRequired();
        this.Property(t => t.Name).HasColumnName("Name").HasMaxLength(255);
        this.Property(t => t.Team).HasColumnName("Team").HasMaxLength(50);
        this.Property(t => t.Position).HasColumnName("Position").HasMaxLength(50);

    }


public FantasyTeam_Mapping() {
        this.HasKey(t => t.FantasyTeamId);
        this.ToTable("FantasyTeam");
        this.Property(t => t.FantasyTeamId).HasColumnName("FantasyTeamId").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        this.Property(t => t.Active).HasColumnName("Active").IsRequired();
        this.Property(t => t.CreatedDate).HasColumnName("CreatedDate").IsRequired();
        this.Property(t => t.LastModified).HasColumnName("LastModified").IsRequired();
        this.Property(t => t.TeamEmail).HasColumnName("TeamEmail").IsRequired();
        this.Property(t => t.TeamName).HasColumnName("TeamName").IsRequired();
        this.HasMany(t => t.Players).WithMany(p => p.FantasyTeams).Map(mc => {
            mc.ToTable("FantasyTeam_2_Player");
            mc.MapLeftKey("PlayerId", "LeagueId");
            mc.MapRightKey("FantasyTeamId");
        });

    }

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,我刚测试了这个(简化版),它没有抛出任何错误:

public class MyContext: DbContext
    {       
        public DbSet<League> Leagues { get; set; }
        public DbSet<Player> Players { get; set; }
        public DbSet<FantasyTeam> FantasyTeams { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<League>().HasKey(m => m.LeagueId);
            modelBuilder.Entity<Player>().HasKey(m => new { m.PlayerId, m.LeagueId })
                .HasRequired<League>(m => m.League);
            modelBuilder.Entity<FantasyTeam>().HasKey(m => m.FantasyTeamId)
                .Property(m => m.FantasyTeamId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<FantasyTeam>().HasMany(t => t.Players).WithMany(p => p.FantasyTeams).Map(mc =>
            {
                mc.ToTable("FantasyTeam_2_Player");
                mc.MapLeftKey("FantasyTeamId");
                mc.MapRightKey("PlayerId", "LeagueId");
            });
            base.OnModelCreating(modelBuilder);
        }
    }