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

Return type of an EF constructor?

The question calls for:

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>

However when you fill in the information gained from the videos it doesn't meet the necessary criteria. Instead you'll get an error message asking for a return type for the EF constructor when not even an example was offered in the previous video. Please help!

Course.cs
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
    public class Course
    {
        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; }

    }
}
Teacher.cs
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {
        public Teachers()
        {
            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; }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

Constructors have no return type.

One identifying factor of a constructor is that is has no return type, not even void. Another identifying factor is that is has the same name as the class.

But in this case, the class is named "Teacher" but you named your constructor "Teachers" (with an extra "s"). This caused the compiler to think you were creating an ordinary method which was missing a type.

Awesome, thanks for the help!