The navigation property 'FootballGame' was not found on the dependent type 'Bd.Domain.Entities.FootballGame'

c# ef-code-first entity-framework

Question

I am creating my first asp.net mvc3 application. I'm using code first methodology. I have the following models:

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

And I have the following class:

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

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

Unfortunately, there is an exception:

The ForeignKeyAttribute on property 'FK_id_FootballGame' on type 'Bd.Domain.FootballGame' is not valid. The navigation property 'FootballGame' was not found on the dependent type 'Bd.Domain.FootballGame'. The Name value should be a valid navigation property name.

I tried to remove these lines:

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

However, another exception to appear:

The ForeignKeyAttribute on property 'id_FootballTeam_owner' on type 'Bd.FootballGame' is not valid. The navigation property 'FootballTeam' was not found on the dependent type 'Bd.FootballGame'. The Name value should be a valid navigation property name.

I look forward to any help. Regards, Denis.

1
7
2/22/2013 8:42:22 PM

Accepted Answer

Try this:

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; }
}
10
2/22/2013 8:40:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow