locked
Entity framework7 codefirst multiple foreigns keys to the same entity RRS feed

  • Question

  • User1543599596 posted
    Hello, i need help creating 2 models with entity framework 7 code first in ASP.NET MVC 6
    Its the basic problem i have the Match class that have an awayteam and hometeam foreign keys from the class Team
    This is how i made the class, and need help with data annotations or the other way.
    Those are the classes

    public class Match
    {
    [Key]
    public int MatchId { get; set; }

    public DateTime playday { get; set; }
    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }

    //public virtual Competition competition { get; set; }


    //public virtual ICollection<Vote> votes { get; set; }





    public virtual Team HomeTeam { get; set; }

    public virtual Team GuestTeam { get; set; }
    }

    public class Team
    {
    public int TeamId { get; set; }
    public String name { get; set; }

    //public virtual ICollection<Player> players { get; set; }

    public virtual ICollection<Match> HomeMatches { get; set; }
    public virtual ICollection<Match> AwayMatches { get; set; }

    }
    Friday, January 29, 2016 6:12 AM

All replies

  • User1724605321 posted

    Hi dennisgzlz.

    Please refer to below thread which provides solution with demo :

    http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table

    Best Regards,

    Nan Yu

    Friday, January 29, 2016 7:39 AM
  • User1543599596 posted

    Hello, this link didn't work for me, as you can see this is the same example from there, that's why I put this here, because non of the solution that they offer there works for me, first using fluent api don't work because the method OnModelCreating(DbModelBuilder modelBuilder) now in this new version of mvc change, now take an object from ModelBuilder instead of DbModelBuilder, so the solution they show there don't work in this new version,

    the other way is using data annotation but didn't get to work either, this article is from 2012 and a lot of things have change since then so, that's why I can't get this to work.

    public class Match
        {
            [Key]
            public int MatchId { get; set; }

            public DateTime playday { get; set; }
            public float HomePoints { get; set; }
            public float GuestPoints { get; set; }

            public int HomeTeamId { get; set; }
            public int GuestTeamId { get; set; }

            [ForeignKey("HomeTeamId")]
            [InverseProperty("HomeMatches")]
            public virtual Team HomeTeam { get; set; }

            [ForeignKey("GuestTeamId")]
            [InverseProperty("AwayMatches")]
            public virtual Team GuestTeam { get; set; }
           
        }

        public class Team
        {
            public int TeamId { get; set; }
            public String name { get; set; }

            public virtual ICollection<Match> HomeMatches { get; set; }
            public virtual ICollection<Match> AwayMatches { get; set; }
        }

    this is the best way that I found, because I can add a new migration and all is ok, but when I update the database I get an error like this

    Introducing FOREIGN KEY constraint 'FK_Match_Team_HomeTeamId' on table 'Match' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
    Could not create constraint or index. See previous errors.

    thanks dennisgzlz88

    Friday, January 29, 2016 8:53 AM