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 and Using a One-to-Many Relationship

Entity Frameworks Basics

Link: https://teamtreehouse.com/library/entity-framework-basics/extending-our-entity-data-model/defining-and-using-a-onetomany-relationship

Question:

In Teacher.cs add the following to the Teacher class:

    A using directive for the System.Collections.Generic namespace
    A navigation collection property named Courses of type ICollection<Course>
    A default constructor that initializes the Courses property to an instance of List<Course>
***

courses.cs
namespace Treehouse.CodeChallenges
{
    public class Course
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }
        public int TeacherId { get; set; } // foreign key property
        public Teacher Teacher { get; set; } // navigation property
    }
}



teacher.cs
using System.Collections.Generic;  // 1st objective
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {
               public Courses() //3rd objective???
        {
            Courses = new List<Course>();
            }
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection<Courses> Courses { get; set; } // 2nd objective
    }
}

error
Teacher.cs(6,23): error CS1520: Class, struct, or interface method must have a return type
Compilation failed: 1 error(s), 0 warnings

What am I missing?

Course.cs
namespace Treehouse.CodeChallenges
{
    public class Course
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }
        public int TeacherId { get; set; } // foreign key property
        public Teacher Teacher { get; set; } // navigation property
    }
}
Teacher.cs
using System.Collections.Generic;  // 1st objective
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {
               public Courses() //3rd objective???
        {
            Courses = new List<Course>();
            }
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection<Courses> Courses { get; set; } // 2nd objective
    }
}

I don't have a clue, guidance please...

4 Answers

Steven Parker
Steven Parker
231,275 Points

I see two issues:

  • Courses should be "type ICollection<Course>" (singular) but you wrote "ICollection<Courses>" (plural)
  • A constructor must have the same name as its class, but you named yours "Courses" instead of "Teacher"

It worked, thanks so much!

Mihai Leonardo Cosmin
Mihai Leonardo Cosmin
3,828 Points

using System.Collections.Generic; //Step 1 namespace Treehouse.CodeChallenges { public class Teacher { public Teacher() { Courses = new List<Course>(); // Step 3 }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Course> Courses{get;set;} // Step 2
}

}

Mihai Leonardo Cosmin
Mihai Leonardo Cosmin
3,828 Points

using System.Collections.Generic; //Step 1 namespace Treehouse.CodeChallenges { public class Teacher { public Teacher() { Courses = new List<Course>(); // Step 3 }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Course> Courses{get;set;} // Step 2
}

}

This is Correct

Steven Parker
Steven Parker
231,275 Points

Did you mean to post it two times? (You can delete the extra one).