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

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

I am confused on how and where to create the constructor Are there any suggestions on a good book to help me understand how to do this correctly?

Course.cs
using System.Collections.Generic

namespace Treehouse.CodeChallenges
{
    public class Course
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public int TeacherId {get; set;}
        public string Description { get; set; }
        public int Length { get; set; }
        public Teacher Teacher {get; set;}

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

        // A default constructor that initializes the Courses property to an instance of List<Course> 
        //Looking for some help in explaining how to do this 

    }
}
Teacher.cs
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

3 Answers

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; }
    }
}

Am I on the right track?

James Churchill
STAFF
James Churchill
Treehouse Teacher

Joe,

For more information on class constructors, check out this video in our C# Objects course:

https://teamtreehouse.com/library/c-objects/objectoriented-programming/object-initialization

If you haven't already done so, you might go ahead and start at the beginning of the C# Objects course and work your way through it. It has a lot of great information about how to use classes in C#.

Thanks ~James

Jacek Bialek
Jacek Bialek
18,225 Points

public Teacher()! no public Teachers()