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 Migrations Getting Started with Using Migrations Using the Configuration Class to Add Seed Data

In Configuration.cs update the Seed method to seed the database with students test data.

Anyone can help me on this, Getting the issue like " Bummer! Did you access the context's 'Students' DbSet property?".

All cs files in same namespace but not sure why the Students Dbset not getting accessed and what wrong in that code snipped.

Configuration.cs
using System.Data.Entity.Migrations;


namespace Treehouse.CodeChallenges
{
    public class Configuration : DbMigrationsConfiguration<Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(Context context)
        {
            #if DEBUG
            context.Students.AddOrUpdate(
            s => s.Id,
            new Student(){Id= 1,FirstName="John",LastName="Smith"},
            new Student(){Id= 2,FirstName="Sally",LastName="Jones"}
            );
            #endif
        }
    }
}
Context.cs
using System.Data.Entity;

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

        public virtual DbSet<Student> Students { get; set; }
    }
}
Student.cs
namespace Treehouse.CodeChallenges
{
    public class Student
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

1 Answer

Steven Parker
Steven Parker
229,744 Points

Your conditional pragma ("#if DEBUG") causes your code to be ignored except when the compiler has DEBUG defined. This is apparently not the case in the challenges.

Just remove the conditional, otherwise the code looks good. :+1:

Thanks Steven for your prompt response, Now its working i got bit confused on the condition part.

Steven Parker
Steven Parker
229,744 Points

Would the person(s) who down-voted this answer please leave some feedback to let me know what it was you didn't like about it? Thanks!