locked
How can I access Entity Framework's generated Foreign Key? RRS feed

  • Question

  • User-1134857695 posted

    Sup people! I have been trying to decipher this for a while... But can't seem to get it.

    I have the following classes:

     public class PGTournament : IImageable
        {
    
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int TournamentId { get; set; }
            public virtual List<Participants.Participant> Participants { get; set; }
          //Other fields omitted for brevity 
         }
    
     public class Participant
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }
    
            public int PGTournament_TournamentId { get; set; }
    
    //Other fields omitted for brevity 
    
        }

    This is the generated schema:

    As you can see, there is a column named "PGTournament_TournamentId1". That column is automatically created by Entity Framework and it holds all the foreign relations with my PGTournament class. I would like to know how can I access it. I tried, as you can see, creating a "PGTournament_TournamentId" field but that did not work. I don't know how to make this work in Fluent API either. It's like doing [ForeignKey("MyKeyName")] but at the reverse...

    Any guesses? I would like to do this so I can perform a single query to the "Participant" repository  without heading to the "PGTournament" repository first.

    Monday, May 9, 2016 12:23 AM

Answers

  • User1559292362 posted

    Hi,

    How can I access Entity Framework's generated Foreign Key?

    Please add a navigate property with foreign key attribute. Like this:

    public class Participant
    
        {
    
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    
            public int Id { get; set; }
    
            public int PGTournament_TournamentId { get; set; }
    
            [ForeignKey("PGTournament_TournamentId")]
    
            public virtual PGTournament PGTournament { get; set; }
    
            //Other fields omitted for brevity
    
    }
    

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 6:01 AM

All replies

  • User1559292362 posted

    Hi,

    How can I access Entity Framework's generated Foreign Key?

    Please add a navigate property with foreign key attribute. Like this:

    public class Participant
    
        {
    
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    
            public int Id { get; set; }
    
            public int PGTournament_TournamentId { get; set; }
    
            [ForeignKey("PGTournament_TournamentId")]
    
            public virtual PGTournament PGTournament { get; set; }
    
            //Other fields omitted for brevity
    
    }
    

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 6:01 AM
  • User-1134857695 posted

    Woohooo! Thanks both for the answer!

    Can't believe the answer was so simple :D :D :D :D

    Tuesday, May 10, 2016 12:23 PM