Entity Framework in ASP.NET MVC Application (二)
In the previous tutorial you created an MVC application that stores and displays data using the Entity Framework and SQL Server Compact. In this tutorial you will review and customize the CRUD (create, read, update, delete) code that the MVC scaffolding automatically creates for you in controllers and views.



The scaffolded code for the Index page left out the Enrollments property, because that property holds a collection. In the Details page you will display the contents of the collection in an HTML table.
In Controllers\StudentController.cs, the action method for the Details view resembles the following example:
Creating a Create PageIn Controllers\StudentController.cs, replace the
HttpPostCreateaction method with the following code to add atry-catchblock to the scaffolded method:Data validation works by default.? Enter names and an invalid date and click Create to see the error message.
In this case you're seeing client-side validation that's implemented using JavaScript. But server-side validation is also implemented. Even if client validation failed, bad data would be caught and an exception would be thrown in server code.
Change the date to a valid value such as 9/1/2005 and click Create to see the new student appear in the Index page.
Creating an Edit Page
In Controllers\StudentController.cs, the
HttpGetEditmethod (the one without theHttpPostattribute) uses theFindmethod to retrieve the selectedStudententity, as you saw in theDetailsmethod. You don't need to change this method.However, replace the
HttpPostEditaction method with the following code to add atry-catchblock:Change some of the data and click Save. You see the changed data in the Index page.
Creating a Delete Page
In Controllers\StudentController.cs, the template code for the
HttpGetDeletemethod uses theFindmethod to retrieve the selectedStudententity, as you saw in theDetailsandEditmethods. However, to implement a custom error message when the call toSaveChangesfails, you will add some functionality to this method and its corresponding view.As you saw for update and create operations, delete operations require two action methods. The method that is called in response to a GET request displays a view that gives the user a chance to approve or cancel the delete operation. If the user approves it, a POST request is created. When that happens, the
HttpPostDeletemethod is called and then that method actually performs the delete operation.You will add a
try-catchblock to theHttpPostDeletemethod to handle any errors that might occur when the database is updated. If an error occurs, theHttpPostDeletemethod calls theHttpGetDeletemethod, passing it a parameter that indicates that an error has occurred. TheHttpGet Deletemethod then redisplays the confirmation page along with the error message, giving the user an opportunity to cancel or to try again.Replace the
HttpGetDeleteaction method with the following code, which manages error reporting:Click Delete. The Index page is displayed without the deleted student. (You'll see an example of the error handling code in action in the Handling Concurrency tutorial later in this series.)
Ensuring that Database Connections Are Not Left OpenTo make sure that database connections are properly closed and the resources they hold freed up, you should see to it that the context instance is disposed. That is why you will find a Dispose method at the end of the
StudentControllerclass in StudentController.cs, as shown in the following example:protected override void Dispose(bool disposing){ db.Dispose(); base.Dispose(disposing);}The base
Controllerclass already implements theIDisposableinterface, so this code simply adds an override to theDispose(bool)method to explicitly dispose the context instance.You now have a complete set of pages that perform simple CRUD operations for
Studententities. In the next tutorial you'll expand the functionality of the Index page by adding sorting and paging.Links to other Entity Framework resources can be found at the end of the last tutorial in this series.


