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

        }
    }
}

4 Answers

Steven Parker
Steven Parker
230,230 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
230,230 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);
    }
}

}

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.Entity<CourseStudent>().Property(c => c.Grade).HasPrecision(4,3);
        }
    }
}