@@ -14,6 +14,8 @@ namespace BookCollection.Controllers
1414{
1515 public class CategoriesController : BaseController
1616 {
17+ public CategoriesController ( IBookRepository rep , IBookContext bc ) : base ( rep , bc ) { /* constructor forward to BaseController */ }
18+
1719 // GET: Categories
1820 public ActionResult Index ( string sortOrder , string currentFilter , string searchString , int ? page , bool noPaging = false )
1921 {
@@ -31,7 +33,7 @@ public ActionResult Index(string sortOrder, string currentFilter, string searchS
3133
3234 ViewBag . CurrentFilter = searchString ;
3335
34- var cats = from s in db . Categories
36+ var cats = from s in db . Query < Category > ( )
3537 select s ;
3638 if ( ! string . IsNullOrEmpty ( searchString ) )
3739 {
@@ -59,13 +61,13 @@ public ActionResult Details(int? id)
5961 {
6062 return RedirectToAction ( "Index" ) ;
6163 }
62- Category category = db . Categories . Find ( id ) ;
64+ Category category = db . Find < Category > ( id ) ;
6365 if ( category == null )
6466 {
6567 return HttpNotFound ( ) ;
6668 } else
6769 {
68- category . Books = db . Books . Where ( b => b . CategoryID == category . CategoryID ) . ToList ( ) ;
70+ category . Books = db . Query < Book > ( ) . Where ( b => b . CategoryID == category . CategoryID ) . ToList ( ) ;
6971 }
7072 return View ( category ) ;
7173 }
@@ -76,14 +78,14 @@ public ActionResult DetailsForName(string name)
7678 {
7779 return RedirectToAction ( "Index" ) ;
7880 }
79- Category category = db . Categories . FirstOrDefault ( b => b . Title == name ) ;
81+ Category category = db . Query < Category > ( ) . FirstOrDefault ( b => b . Title == name ) ;
8082 if ( category == null )
8183 {
8284 return HttpNotFound ( ) ;
8385 }
8486 else
8587 {
86- category . Books = db . Books . Where ( b => b . CategoryID == category . CategoryID ) . ToList ( ) ;
88+ category . Books = db . Query < Book > ( ) . Where ( b => b . CategoryID == category . CategoryID ) . ToList ( ) ;
8789 }
8890 return View ( "Details" , category ) ;
8991 }
@@ -103,7 +105,7 @@ public ActionResult Create([Bind(Include = "CategoryID,Title")] Category categor
103105 {
104106 if ( ModelState . IsValid )
105107 {
106- db . Categories . Add ( category ) ;
108+ db . Add ( category ) ;
107109 db . SaveChanges ( ) ;
108110 return RedirectToAction ( "Index" ) ;
109111 }
@@ -118,7 +120,7 @@ public ActionResult Edit(int? id)
118120 {
119121 return new HttpStatusCodeResult ( HttpStatusCode . BadRequest ) ;
120122 }
121- Category category = db . Categories . Find ( id ) ;
123+ Category category = db . Find < Category > ( id ) ;
122124 if ( category == null )
123125 {
124126 return HttpNotFound ( ) ;
@@ -135,7 +137,7 @@ public ActionResult Edit([Bind(Include = "CategoryID,Title")] Category category)
135137 {
136138 if ( ModelState . IsValid )
137139 {
138- db . Entry ( category ) . State = EntityState . Modified ;
140+ db . SetState ( category , EntityState . Modified ) ;
139141 db . SaveChanges ( ) ;
140142 return RedirectToAction ( "Index" ) ;
141143 }
@@ -149,7 +151,7 @@ public ActionResult Delete(int? id)
149151 {
150152 return new HttpStatusCodeResult ( HttpStatusCode . BadRequest ) ;
151153 }
152- Category category = db . Categories . Find ( id ) ;
154+ Category category = db . Find < Category > ( id ) ;
153155 if ( category == null )
154156 {
155157 return HttpNotFound ( ) ;
@@ -162,8 +164,8 @@ public ActionResult Delete(int? id)
162164 [ ValidateAntiForgeryToken ]
163165 public ActionResult DeleteConfirmed ( int id )
164166 {
165- Category category = db . Categories . Find ( id ) ;
166- db . Categories . Remove ( category ) ;
167+ Category category = db . Find < Category > ( id ) ;
168+ db . Remove ( category ) ;
167169 db . SaveChanges ( ) ;
168170 return RedirectToAction ( "Index" ) ;
169171 }
0 commit comments