0

EF 在 4.3.1 上使用代码优先以下是幻想体育工具的简单模式:

架构

这是一个带有转折的经典多对多关系。Player 表有一个 PlayerId+LeagueId 的复合键,以便转移到联结表。(注意:PlayerId 在表中不是唯一的,给定的联赛(例如 NFL)可能使用与不同联赛相同的 playerIds(例如 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”无效。指定的列数必须与主键列数匹配。

编辑:这里是实体

梦幻团队

    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");
        });

    }

我在这里做错了什么?

4

1 回答 1

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);
        }
    }
于 2013-09-06T00:33:48.373 回答