Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Entity Framework Basics Extending Our Entity Data Model Defining a Many-to-Many Relationship with an Explicit Bridge Entity

Brendon Brooks
Brendon Brooks
8,802 Points

Foreign Key Error

For some reason I keep on getting an error that states that the foreign key in which I'm using does not exist? Did I miss a step here?

Course.cs
using System.Collections.Generic;

namespace Treehouse.CodeChallenges
{
    public class Course
    {
        public Course()
        {
            Students = new List<Student>();
        }

        public int Id { get; set; }
        public int TeacherId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }

        public Teacher Teacher { get; set; }
        public ICollection<Student> Students { get; set; }
    }
}
CourseStudent.cs
namespace Treehouse.CodeChallenges
{
    public class CourseStudent
    {
        public int Id { get; set; }
        public int CourseId { get; set; }
        [ForeignKey("CourseId")]
        public int StudentID { get; set; }
        [ForeignKey("StudentId")]
        public decimal Grade { get; set; }

        public  Course Course { get; set; }
        public  Student Student { get; set; }




    }
}
Student.cs
using System.Collections.Generic;

namespace Treehouse.CodeChallenges
{
    public class Student
    {
        public Student()
        {
            Courses = new List<Course>();
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public ICollection<Course> Courses { get; set; }
    }
}

3 Answers

Simon Owerien
Simon Owerien
12,829 Points
public class CourseStudent
    {
        public int Id { get; set; }
        public int CourseId { get; set; }
        [ForeignKey("CourseId")] 
        public int StudentID { get; set; }
        [ForeignKey("StudentId")]      
        public decimal Grade { get; set; }

        public  Course Course { get; set; }
        public  Student Student { get; set; }
    }

You are using the DataAnnotations on the wrong line. You put C#-Attributes ahead of the variable you want to apply the attribute to:

[ForeignKey("CourseId")]        
 public int CourseId { get; set; }
[ForeignKey("StudentId")] 
public int StudentID { get; set; }
Steven Parker
Steven Parker
231,269 Points

I almost mentioned that too, but fixing it still won't pass the challenge.

Steven Parker
Steven Parker
231,269 Points

I passed this challenge without using the data annotations.

If you did need them, you'd also need a "using System.ComponentModel.DataAnnotations;", but I tried that and it didn't help. So just don't use the annotations.


Addendum: the course asked for a field named "StudentId" (with a lower-case "d"), but you wrote "StudentID" (with a capital "D").

Brendon Brooks
Brendon Brooks
8,802 Points

Same here. I've tried both methods and they are not passing

Steven Parker
Steven Parker
231,269 Points

Don't forget to fix the spelling of "StudentId".