7

我正在创建我的第一个 asp.net mvc3 应用程序。我正在使用代码优先方法。我有以下型号:

public class FootballGame
{
    [Key]
    public Guid id_FootballGame { get; set; }

    [ForeignKey("FootballGame")]
    public Guid? FK_id_FootballGame { get; set; }
    public virtual FootballGame PreviousFootballGame { get; set; }

    [ForeignKey("FootballTeam")]
    public Guid id_FootballTeam_owner { get; set; }
    public virtual FootballTeam FootballTeamOwner { get; set; }

    [ForeignKey("FootballTeam")]
    public Guid id_FootballTeam_guest { get; set; }
    public virtual FootballTeam FootballTeamGuest { get; set; }
}

public class FootballTeam
{
    [Key]
    public Guid id_FootballTeam { get; set; }
    public string teamName { get; set; }
}

我有以下课程:

public class EFDbContext : DbContext
{
    public EFDbContext() : base("name=EFDbContext") { }

    public DbSet<FootballTeam> FootballTeams { get; set; }
    public DbSet<FootballGame> FootballGames { get; set; }
}

不幸的是,有一个例外:

类型“Bd.Domain.FootballGame”的属性“FK_id_FootballGame”上的 ForeignKeyAttribute 无效。在依赖类型“Bd.Domain.FootballGame”上找不到导航属性“FootballGame”。Name 值应该是有效的导航属性名称。

我试图删除这些行:

[ForeignKey("FootballGame")]
public virtual FootballGame PreviousFootballGame { get; set; }

但是,出现另一个例外:

类型“Bd.FootballGame”的属性“id_FootballTeam_owner”上的 ForeignKeyAttribute 无效。在依赖类型“Bd.FootballGame”上找不到导航属性“FootballTeam”。Name 值应该是有效的导航属性名称。

我期待任何帮助。问候,丹尼斯。

4

1 回答 1

10

试试这个:

public class FootballGame
{
    [Key]
    public Guid id_FootballGame { get; set; }

    public Guid? FK_id_FootballGame { get; set; }
    [ForeignKey("FK_id_FootballGame")]
    public virtual FootballGame PreviousFootballGame { get; set; }

    public Guid id_FootballTeam_owner { get; set; }
    [ForeignKey("id_FootballTeam_owner")]
    public virtual FootballTeam FootballTeamOwner { get; set; }

    public Guid id_FootballTeam_guest { get; set; }
    [ForeignKey("id_FootballTeam_guest")]
    public virtual FootballTeam FootballTeamGuest { get; set; }
}
于 2013-02-22T20:40:36.450 回答