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 trialAmy Shah
25,337 Pointsi might be very close now
In Repository.cs update the UpdateCourse method to persist changes to a course to the database. Instantiate an instance of the Context class within a using statement Call the Attach method on the context's Courses DbSet property to attach the course parameter to the context Retrieve the course's entry using the context's Entry method and set its state to "Modified" Call the context's SaveChanges method to persist the changes to the database
I am not sure I set the state to modified correctly
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace Treehouse.CodeChallenges
{
public static class Repository
{
public static List<Course> GetCourses()
{
using (var context = new Context())
{
return context.Courses
.OrderBy(c => c.Teacher.LastName)
.ThenBy(c => c.Teacher.FirstName)
.ToList();
}
}
public static List<Course> GetCoursesByTeacher(string lastName)
{
using (var context = new Context())
{
return context.Courses
.Where(c => c.Teacher.LastName == lastName)
.ToList();
}
}
public static Course GetCourse(int id)
{
using (var context = new Context())
{
return context.Courses
.Include(c => c.Teacher)
.SingleOrDefault(c => c.Id == id);
}
}
public static void AddCourse(Course course)
{
using (var context = new Context())
{
context.Courses.Add(course);
context.SaveChanges();
}
}
public static void UpdateCourse(Course course)
{
// TODO
using (var context = new Context())
{
context.Courses.Attach(course)
.Entry("Modified");
context.SaveChanges();
}
}
}
1 Answer
Steven Parker
231,198 PointsWell, maybe close...er anyway.
- it looks like your're missing the close brace of your using block
- like SaveChanges, Entry is a method of the context itself
- the Entry method returns a DbEntityEntry object that has a State property
- the State property can be set, but not using a string
- check the EntityState documentation for more info on possible values.