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

混江龙づ霸主 提交于 2019-12-22 04:46:37

问题


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:


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


来源:https://stackoverflow.com/questions/15032656/the-navigation-property-footballgame-was-not-found-on-the-dependent-type-bd-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!