Getting Started with the Entity Framework 4.1 using ASP.NET MVC.pdf
(
11065 KB
)
Pobierz
Getting Started with the Entity
Framework 4.1 Using ASP.NET MVC
Tom Dykstra
Summary
: In this book, you'll learn the basics of using Entity Framework Code First to
display and edit data in an ASP.NET MVC application.
Category:
Step-by-Step
Applies to
: ASP.NET 4.0, MVC 3, Entity Framework 4.1, Visual Studio 2010
Source
: ASP.NET site
(
link to source content
)
E-book publication date
: June 2012
Copyright © 2012 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means
without the written permission of the publisher.
Microsoft and the trademarks listed at
http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx
are trademarks of the
Microsoft group of companies. All other marks are property of their respective owners.
The example companies, organizations, products, domain names, email addresses, logos, people, places, and events
depicted herein are fictitious. No association with any real company, organization, product, domain name, email address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
2
Contents
Creating an Entity Framework Data Model for an ASP.NET MVC Application.............................................................7
The Contoso University Web Application.........................................................................................................................7
Entity Framework Development Approaches .............................................................................................................. 11
Database First ........................................................................................................................................................................... 12
Model First ................................................................................................................................................................................. 12
Code First ................................................................................................................................................................................... 13
POCO (Plain Old CLR Objects) ........................................................................................................................................... 13
Creating an MVC Web Application .................................................................................................................................. 13
Setting Up the Site Style....................................................................................................................................................... 15
Creating the Data Model...................................................................................................................................................... 18
The Student Entity................................................................................................................................................................... 19
The Enrollment Entity ............................................................................................................................................................ 21
The Course Entity .................................................................................................................................................................... 22
Creating the Database Context ......................................................................................................................................... 23
Setting the Connection String............................................................................................................................................ 24
Initializing the Database with Test Data......................................................................................................................... 24
Creating a Student Controller ............................................................................................................................................ 27
Conventions............................................................................................................................................................................... 33
Implementing Basic CRUD Functionality.................................................................................................................................. 35
Creating a Details Page......................................................................................................................................................... 39
Creating a Create Page ......................................................................................................................................................... 43
Creating an Edit Page ............................................................................................................................................................ 47
Entity States and the Attach and SaveChanges Methods....................................................................................... 48
Creating a Delete Page ......................................................................................................................................................... 51
Ensuring that Database Connections Are Not Left Open ....................................................................................... 56
Sorting, Filtering, and Paging ....................................................................................................................................................... 57
Adding Column Sort Links to the Students Index Page .......................................................................................... 58
Adding Sorting Functionality to the Index Method .................................................................................................. 58
3
Adding Column Heading Hyperlinks to the Student Index View......................................................................... 60
Adding a Search Box to the Students Index Page ..................................................................................................... 62
Adding Filtering Functionality to the Index Method................................................................................................. 62
Adding a Search Box to the Student Index View........................................................................................................ 63
Adding Paging to the Students Index Page ................................................................................................................. 64
Installing the PagedList NuGet Package ........................................................................................................................ 65
Adding Paging Functionality to the Index Method ................................................................................................... 66
Adding Paging Links to the Student Index View ........................................................................................................ 69
Creating an About Page That Shows Student Statistics .......................................................................................... 73
Creating the View Model ..................................................................................................................................................... 74
Modifying the Home Controller ........................................................................................................................................ 74
Modifying the About View .................................................................................................................................................. 75
Creating a More Complex Data Model..................................................................................................................................... 78
Using Attributes to Control Formatting, Validation, and Database Mapping ................................................ 79
The DisplayFormat Attribute............................................................................................................................................... 79
The MaxLength Attribute ..................................................................................................................................................... 81
The Column Attribute............................................................................................................................................................ 83
Creating the Instructor Entity ............................................................................................................................................. 85
The Required and Display Attributes .............................................................................................................................. 86
The FullName Calculated Property................................................................................................................................... 86
The Courses and OfficeAssignment Navigation Properties ................................................................................... 87
Creating the OfficeAssignment Entity............................................................................................................................. 87
The Key Attribute .................................................................................................................................................................... 88
The Instructor Navigation Property ................................................................................................................................. 88
Modifying the Course Entity ............................................................................................................................................... 89
The DatabaseGenerated Attribute.................................................................................................................................... 90
Foreign Key and Navigation Properties ......................................................................................................................... 90
Creating the Department Entity ........................................................................................................................................ 91
The Column Attribute............................................................................................................................................................ 92
Foreign Key and Navigation Properties ......................................................................................................................... 92
Modifying the Student Entity ............................................................................................................................................. 93
Modifying the Enrollment Entity ....................................................................................................................................... 94
4
Plik z chomika:
wiklef8
Inne pliki z tego folderu:
802.11_wireless_networks_the_definitive_guide_second_edition.pdf
(9934 KB)
beginning_sharepoint_designer_2010.pdf
(18859 KB)
Claims-based Identity Second Edition device.pdf
(19784 KB)
5TSSSISU_SQL_Server_2012.pdf
(659 KB)
AGM_Application_Virtualization_(App-V)_5.0.pdf
(1605 KB)
Inne foldery tego chomika:
book
books 1
Complete IT Books Pack
Exams
Kursy
Zgłoś jeśli
naruszono regulamin