Using Code First Migrations to Deploy the Database .................................................................. 103
Get a Windows Azure account ....................................................................................................... 103
Create a web site and a SQL database in Windows Azure ......................................................... 103
Deploy the application to Windows Azure.................................................................................... 107
Advanced Migrations Scenarios ........................................................................................................ 122
Code First Initializers ......................................................................................................................... 122
Summary .............................................................................................................................................. 123
Creating a More Complex Data Model for an ASP.NET MVC Application .............................................. 124
Customize the Data Model by Using Attributes ............................................................................... 125
The DataType Attribute ................................................................................................................. 126
The StringLengthAttribute ............................................................................................................ 128
The Column Attribute .................................................................................................................... 130
Complete Changes to the Student Entity .......................................................................................... 132
The Required Attribute .................................................................................................................. 133
The Display Attribute ..................................................................................................................... 134
The FullName Calculated Property .............................................................................................. 134
Create the Instructor Entity .............................................................................................................. 134
The Courses and OfficeAssignment Navigation Properties ........................................................ 135
Create the OfficeAssignment Entity .................................................................................................. 136
The Key Attribute ........................................................................................................................... 137
The ForeignKey Attribute .............................................................................................................. 137
The Instructor Navigation Property ............................................................................................. 137
Modify the Course Entity ................................................................................................................... 138
The DatabaseGenerated Attribute ................................................................................................ 139
Foreign Key and Navigation Properties ........................................................................................ 139
Create the Department Entity ........................................................................................................... 139
The Column Attribute .................................................................................................................... 140
Foreign Key and Navigation Properties ........................................................................................ 141
Modify the Enrollment Entity ............................................................................................................ 141
Foreign Key and Navigation Properties ........................................................................................ 142
Many-to-Many Relationships ......................................................................................................... 142
Entity Diagram Showing Relationships ............................................................................................ 145
Customize the Data Model by adding Code to the Database Context............................................ 147
Seed the Database with Test Data ..................................................................................................... 148
Add a Migration and Update the Database ...................................................................................... 154
Summary .............................................................................................................................................. 157
Reading Related Data with the Entity Framework in an ASP.NET MVC Application ............................. 158
Lazy, Eager, and Explicit Loading of Related Data ........................................................................ 160
Performance considerations ........................................................................................................... 161
Disable lazy loading before serialization ....................................................................................... 161
Create a Courses Page That Displays Department Name ............................................................... 162
Create an Instructors Page That Shows Courses and Enrollments ............................................... 165
Create a View Model for the Instructor Index View ................................................................... 168
Create the Instructor Controller and Views ................................................................................. 168
Modify the Instructor Index View ................................................................................................. 171
Adding Explicit Loading ................................................................................................................ 178
Summary .............................................................................................................................................. 179
Updating Related Data with the Entity Framework in an ASP.NET MVC Application ........................... 180
Customize the Create and Edit Pages for Courses .......................................................................... 183
Adding an Edit Page for Instructors ................................................................................................. 191
Adding Course Assignments to the Instructor Edit Page ................................................................ 195
Update the DeleteConfirmed Method ............................................................................................... 205
Add office location and courses to the Create page ......................................................................... 205
Handling Transactions ....................................................................................................................... 209
Summary .............................................................................................................................................. 209
Async and Stored Procedures with the Entity Framework in an ASP.NET MVC Application ................ 210
Why bother with asynchronous code ................................................................................................ 212
Create the Department controller ..................................................................................................... 213
Use stored procedures for inserting, updating, and deleting .......................................................... 217
Deploy to Windows Azure .................................................................................................................. 221
Summary .............................................................................................................................................. 222
Handling Concurrency with the Entity Framework 6 in an ASP.NET MVC 5 Application (10 of 12) ...... 223
Concurrency Conflicts ........................................................................................................................ 224
Pessimistic Concurrency (Locking) ............................................................................................... 225
Optimistic Concurrency ................................................................................................................. 225
Detecting Concurrency Conflicts ................................................................................................... 228
Add an Optimistic Concurrency Property to the Department Entity........................................... 229
Modify the Department Controller ................................................................................................... 230
Testing Optimistic Concurrency Handling ....................................................................................... 233
Updating the Delete Page ................................................................................................................... 240
Summary .............................................................................................................................................. 248
Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application (11 of 12)249
Options for mapping inheritance to database tables ....................................................................... 249
Create the Person class ....................................................................................................................... 251
Make Student and Instructor classes inherit from Person .............................................................. 252
Add the Person Entity Type to the Model ........................................................................................ 253
Create and Update a Migrations File ................................................................................................ 253
Testing .................................................................................................................................................. 255
Deploy to Windows Azure .................................................................................................................. 258
Summary .............................................................................................................................................. 260
Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12) ............................ 261
Performing Raw SQL Queries ........................................................................................................... 263
Calling a Query that Returns Entities ........................................................................................... 264
Calling a Query that Returns Other Types of Objects ................................................................ 265
Calling an Update Query ................................................................................................................ 267
No-Tracking Queries .......................................................................................................................... 273
Examining SQL sent to the database ................................................................................................ 278
Repository and unit of work patterns ............................................................................................... 283
Proxy classes ........................................................................................................................................ 284
Automatic change detection ............................................................................................................... 286
Automatic validation .......................................................................................................................... 286
Entity Framework Power Tools......................................................................................................... 286
Entity Framework source code .......................................................................................................... 289
Summary .............................................................................................................................................. 289
Acknowledgments ............................................................................................................................... 289
VB ......................................................................................................................................................... 289
Common errors, and solutions or workarounds for them .................................................................. 289
Cannot create/shadow copy............................................................................................................ 290
Update-Database not recognized ................................................................................................... 290