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 trialjared eiseman
29,023 PointsNot sure I understand what's going on here...
The coding challenge says it won't compile... After reviewing the video and other similar questions... I don't get what I'm doing wrong here...
The prompt is: 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
public static void UpdateCourse(Course course)
{
using (Context context = new Context())
{
context.Courses.Attach(course);
context.Entry(course).State = EntityState.Modified;
context.SaveChanges();
}
}
1 Answer
Steven Parker
231,248 PointsApparently (based on the error message) instantiating a "new Context()" creates a ContextProxy object. I admit I'm a bit baffled by that myself. But if you substitute that for the type of "context" you'll pass. An even easier solution is to just use "var" and let the compiler determine the type:
using (var context = new Context())
Perhaps James Churchill might help us understand the Context
vs. ContextProxy
thing.