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 Refining a Model Using Data Annotations

Entity Framework Basics

Link: https://teamtreehouse.com/library/entity-framework-basics/extending-our-entity-data-model/refining-a-model-using-data-annotations

Question:
In Course.cs add data annotation attributes to the Title property in order to make the following model refinements:

    A value will be required and the associated database column will not allow null values
    The string value will not exceed 200 characters in length and the associated database column data type will be nvarchar(200)

Code Given:
Course.cs
using System.Collections.Generic;

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

        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<CourseStudent> Students { get; set; }
    }
}
Error: 
Bummer! Did you add the 'using' directive for the System.ComponentModel.DataAnnotations namespace?
My Answer:
using System.Collections.Generic;

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

        public int Id { get; set; }
        public int TeacherId { get; set; }
        public string Title { get; set; }
        [Required, StringLength(200)]
        public string Description { get; set; }
        public int Length { get; set; }

        public Teacher Teacher { get; set; }
        public ICollection<CourseStudent> Students { get; set; }
    }
}
Course.cs
using System.Collections.Generic;

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

        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<CourseStudent> Students { get; set; }
    }
}

The error appears to be asking me to add a line of code that has nothing to do with what is asked by the question, is this the case or am I missing something?

7 Answers

My apologies Im frustrated and taking it out on you

Steven Parker
Steven Parker
229,981 Points

Larry Singleton β€” If you're no longer frustrated, perhaps you would select "best answer" as the one that was most helpful to resolving the issue. :smile:

If the namespace is needed it should either be already there or a part of the question or asked for in the question. It confuses those of us trying to figure this out. I've had a lot of issues in this course with questions not being specific and found myself over doing answers. It's quite irritating. It will be covered in the course review. I do realize it may be me which would be nothing new.

Thanks for the guidance, I will retry the challenge using your suggestions.

Steven Parker
Steven Parker
229,981 Points

To me, having to add the namespace seemed like a realistic real-world scenario one might encounter in development.

using System.ComponentModel.DataAnnotations; using System.Collections.Generic;

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

    public int Id { get; set; }
    public int TeacherId { get; set; }
    [Required,StringLength(200)]
    public string Title { get; set; }
    [Required,StringLength(200)]
    public string Description { get; set; }
    public int Length { get; set; }

    public Teacher Teacher { get; set; }
    public ICollection<CourseStudent> Students { get; set; }
}

}

Get right answer which I have posted above.

Steven Parker
Steven Parker
229,981 Points

That namespace is required when using data annotations.

So it's not unrelated, it's an important part of the challenge.

Also, the annotations must come before the item they apply to. You've applied them to the Description here instead of the Title.

Where is this explained?

A property named Grade of type decimal

Steven Parker
Steven Parker
229,981 Points

Out of context that sounds like just a basic object property, like this:

        public decimal Grade { get; set; }

It should be a part of the question if its required, not assumed the student knows to place it there.

Steven Parker
Steven Parker
229,981 Points

That's a good point, and I've seen that done in other challenges. You might want to make a suggestion to Support about that.

I'll add it in the course review I'll be filling out after this course is over. Again, thank you for all your assistance you are making this a bit clearer. It's still muddy waters tho =P