Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# Entity Framework Basics Extending Our Entity Data Model Using the Fluent API

In Context.cs use the Fluent API in the OnModelCreating method to configure the CourseStudent.Grade property to have a p

I tried to copy his method for this challenge but I keep getting an error message. Not sure what I'm doing wrong.

Context.cs
using System.Data.Entity;

namespace Treehouse.CodeChallenges
{
    public class Context : DbContext
    {
        public Context()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
        }

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
            modelBuilder.Conventions.Add (new DecimalPropertyConvention)(4, 3);

            modelBuilder.Entity<CourseStudent.Grade>()
            .Property(cb => cb.AverageRating)
            .HasPrecision(4, 3);

        }
    }
}

3 Answers

Steven Parker
Steven Parker
217,574 Points

Here's a few hints:

  • for best results with challenges, do only what the challenge asks
  • challenges will generaly not be exactly like a video example
  • the challenge did not ask for any changes to Conventions
  • be sure the type argument to Entity is an entity and not a property
  • be sure to access the correct property via the "Property" method
Steven Parker
Steven Parker
217,574 Points

Closer, but still a couple of issues remain:

  • be sure the type argument to Entity is an entity and not a property

modelBuilder.Entity<CourseStudent.Grade>() :point_left: "CourseStudent" is the entity, "Grade" is the property

  • be sure to access the correct property via the "Property" method

.Property(cb => cb.AverageRating) :point_left: the property needed here is not "AverageRating"

Okay I took away the unnecessary code but still get the error "Did you call the model builder's 'Entity' method?"

using System.Data.Entity;

namespace Treehouse.CodeChallenges { public class Context : DbContext { public Context() { Database.SetInitializer(new DropCreateDatabaseAlways<Context>()); }

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity<CourseStudent.Grade>()
        .Property(cb => cb.AverageRating)
        .HasPrecision(4, 3);
    }
}

}