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

Not sure what I am doing wrong with this one? As far as I can tell I followed along with the video example to a <T>.

I tried two different ways of coding for this code challenge, I was unable to pass using either coding method as illustrated in the video. I included a comment in my code to show the other way that I tried to code to pass the challenge.

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)
        {
                //const int studentId1 = 1;
                //const int studentId2 = 2;

                context.Students.AddorUpdate(
                    s => s.Id,
                    new Student() {Id=1, FirstName = "John", LastName = "Smith"}
                    new Student() {Id=2, FirstName = "Sally", LastName = "Jones"}
                );

            //Tried two different ways the first way I tried involved using the constants above with the constant field names placed where the
            //Id fields are.  e.g.  new Student() {Id=studentId1, FirstName = "John", LastName = "Smith"}


//Make a call to the context's Students DbSet property's AddOrUpdate method
//For the first parameter, pass in a lambda expression that returns the Student entity class' Id property as the property that EF should use to determine if an add or update should be done
//Pass in a Student entity object instance with the values "1", "John", and "Smith" respectively for the properties Id, FirstName, and LastName
//Pass in a second Student entity object instance with the values "2", "Sally", and "Jones" respectively for the properties Id, FirstName, and LastName
            // TODO Add student test data.
        }
    }
}
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; }
    }
}

3 Answers

Steven Parker
Steven Parker
231,248 Points

You're very close! Just two tiny errors:

  • you wrote "AddorUpdate" (with small "o") instead of "AddOrUpdate" (with capital "O")
  • you're missing a comma after the second parameter (first Student object)

Great job Fouts!

Thank you Amy.

Removed, I didn't mean to step over the line.