About the Author |
|
xix | |
About the Technical Reviewer |
|
xxi | |
Part I: Introducing Entity Framework Core 2 |
|
1 | (202) |
|
Chapter 1 Entity Framework Core in Context |
|
|
3 | (4) |
|
Understanding Entity Framework Core |
|
|
3 | (1) |
|
|
4 | (1) |
|
What Do You Need to Know? |
|
|
4 | (1) |
|
What Software Do You Need? |
|
|
4 | (1) |
|
What If You Don't Want to Use Windows? |
|
|
4 | (1) |
|
What Is the Structure of This Book? |
|
|
4 | (1) |
|
Part 1: Introducing Entity Framework Core 2 |
|
|
5 | (1) |
|
Part 2: Entity Framework Core 2 in Detail |
|
|
5 | (1) |
|
Part 3: Advanced Entity Framework Core 2 |
|
|
5 | (1) |
|
Where Can You Get the Example Code? |
|
|
5 | (1) |
|
Where Can You Get Corrections for This Book? |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
5 | (2) |
|
Chapter 2 Your First Entity Framework Core Application |
|
|
7 | (18) |
|
|
7 | (3) |
|
|
8 | (1) |
|
Installing Visual Studio 2017 |
|
|
8 | (2) |
|
|
10 | (1) |
|
|
10 | (1) |
|
|
11 | (3) |
|
Creating the Data Model and Context Classes |
|
|
14 | (1) |
|
Creating the Controller and Views |
|
|
15 | (4) |
|
Configuring Entity Framework Core |
|
|
19 | (2) |
|
Configuring the Connection String |
|
|
20 | (1) |
|
Configuring the Startup Class |
|
|
21 | (1) |
|
|
22 | (1) |
|
|
22 | (2) |
|
|
24 | (1) |
|
Chapter 3 Working with Databases |
|
|
25 | (24) |
|
Preparing for This Chapter |
|
|
26 | (1) |
|
|
27 | (6) |
|
Examining the Database Tables |
|
|
29 | (2) |
|
Examining the Database Content |
|
|
31 | (2) |
|
|
33 | (12) |
|
|
33 | (9) |
|
Storing and Updating Data |
|
|
42 | (2) |
|
|
44 | (1) |
|
|
45 | (2) |
|
|
45 | (2) |
|
|
47 | (1) |
|
|
47 | (2) |
|
Chapter 4 SportsStore: A Real (Data) Application |
|
|
49 | (12) |
|
|
49 | (9) |
|
Configuring the MVC Framework |
|
|
51 | (1) |
|
|
52 | (1) |
|
|
52 | (2) |
|
Adding a Controller and View |
|
|
54 | (2) |
|
Adding the Finishing Touches |
|
|
56 | (2) |
|
Running the Example Application |
|
|
58 | (1) |
|
|
59 | (2) |
|
Chapter 5 SportsStore: Storing Data |
|
|
61 | (22) |
|
Preparing for this Chapter |
|
|
61 | (1) |
|
Configuring Entity Framework Core |
|
|
62 | (1) |
|
Configuring Entity Framework Core Logging Messages |
|
|
62 | (1) |
|
|
63 | (2) |
|
Defining a Primary Key Property |
|
|
63 | (1) |
|
Creating the Database Context Class |
|
|
63 | (1) |
|
Updating the Repository Implementation |
|
|
64 | (1) |
|
|
65 | (4) |
|
Configuring the Connection String |
|
|
65 | (1) |
|
Configuring the Database Provider and Context Class |
|
|
66 | (2) |
|
|
68 | (1) |
|
|
69 | (1) |
|
Avoiding the Query Pitfalls |
|
|
70 | (8) |
|
Understanding the IEnumerable T Pitfall |
|
|
71 | (2) |
|
Understanding the Duplicate Query Pitfall |
|
|
73 | (5) |
|
Common Problems and Solutions |
|
|
78 | (4) |
|
Problems Creating or Accessing the Database |
|
|
78 | (2) |
|
|
80 | (1) |
|
|
81 | (1) |
|
|
82 | (1) |
|
Chapter 6 SportsStore: Modifying and Deleting Data |
|
|
83 | (22) |
|
Preparing for This Chapter |
|
|
83 | (2) |
|
|
85 | (14) |
|
|
85 | (1) |
|
Updating the Controller and Creating a View |
|
|
86 | (5) |
|
Updating Only Changed Properties |
|
|
91 | (1) |
|
|
92 | (7) |
|
|
99 | (4) |
|
Common Problems and Solutions |
|
|
103 | (1) |
|
Objects Are Not Updated or Deleted |
|
|
103 | (1) |
|
The "Reference Not Set to an Instance of an Object" Exception |
|
|
103 | (1) |
|
The "Instance of Entity Type Cannot be Tracked" Exception |
|
|
104 | (1) |
|
The "Property Has a Temporary Value" Exception |
|
|
104 | (1) |
|
Updates Result in Zero Values |
|
|
104 | (1) |
|
|
104 | (1) |
|
Chapter 7 SportsStore: Expanding the Model |
|
|
105 | (32) |
|
Preparing for This Chapter |
|
|
105 | (2) |
|
Creating a Data Model Relationship |
|
|
107 | (9) |
|
Adding a Data Model Class |
|
|
108 | (1) |
|
Creating the Relationship |
|
|
108 | (1) |
|
Updating the Context and Creating the Repository |
|
|
109 | (2) |
|
Creating and Applying a Migration |
|
|
111 | (1) |
|
Creating a Controller and View |
|
|
111 | (4) |
|
Populating the Database with Categories |
|
|
115 | (1) |
|
Using a Data Relationship |
|
|
116 | (6) |
|
Working with Related Data |
|
|
116 | (2) |
|
Selecting a Category for a Product |
|
|
118 | (2) |
|
Creating and Editing Products with Categories |
|
|
120 | (2) |
|
Adding Support for Orders |
|
|
122 | (11) |
|
Creating the Data Model Classes |
|
|
122 | (1) |
|
Creating the Repository and Preparing the Database |
|
|
123 | (2) |
|
Creating Controllers and Views |
|
|
125 | (6) |
|
|
131 | (2) |
|
Common Problems and Solutions |
|
|
133 | (2) |
|
The "ALTER TABLE conflicted with the FOREIGN KEY" Exception |
|
|
133 | (1) |
|
The "UPDATE Conflicted with the FOREIGN KEY" Exception |
|
|
134 | (1) |
|
The "The Property Expression x = x name is Not Valid Exception |
|
|
134 | (1) |
|
The "Type of Navigation Property name Does Not Implement ICollection OrderLine Exception |
|
|
134 | (1) |
|
The "The Property name is Not a Navigation Property of Entity Type name Exception |
|
|
134 | (1) |
|
The "Invalid Object Name name Exception |
|
|
134 | (1) |
|
Objects Are Deleted Instead of Being Updated |
|
|
135 | (1) |
|
The Class Name for Related Data Is Displayed in a View |
|
|
135 | (1) |
|
|
135 | (2) |
|
Chapter 8 SportsStore: Scaling Up |
|
|
137 | (24) |
|
Preparing for This Chapter |
|
|
137 | (5) |
|
Creating a Seed Data Controller and View |
|
|
137 | (5) |
|
Scaling Up Data Presentation |
|
|
142 | (13) |
|
Adding Support for Pagination |
|
|
142 | (6) |
|
Adding Search and Ordering Support |
|
|
148 | (4) |
|
Applying the Data Presentation Features to Categories |
|
|
152 | (3) |
|
|
155 | (4) |
|
Creating and Applying Indexes |
|
|
157 | (2) |
|
Common Problems and Solutions |
|
|
159 | (1) |
|
Queries for Pages Are Too Slow |
|
|
159 | (1) |
|
Applying the Index Migration Times Out |
|
|
159 | (1) |
|
Creating an Index Does Not Improve Performance |
|
|
160 | (1) |
|
|
160 | (1) |
|
Chapter 9 SportsStore: Customer Features |
|
|
161 | (24) |
|
Preparing for This Chapter |
|
|
161 | (6) |
|
Removing the Timing Statements |
|
|
161 | (1) |
|
|
162 | (1) |
|
|
162 | (1) |
|
|
163 | (3) |
|
|
166 | (1) |
|
Displaying Products to the Customer |
|
|
167 | (7) |
|
|
167 | (2) |
|
Creating the Store Controller, Views, and Layout |
|
|
169 | (4) |
|
Testing the Store Display |
|
|
173 | (1) |
|
|
174 | (9) |
|
Enabling Persistent Session Data |
|
|
174 | (3) |
|
Creating the Cart Model Class |
|
|
177 | (1) |
|
Creating the Controller and Views |
|
|
178 | (5) |
|
Testing the Ordering Process |
|
|
183 | (1) |
|
Common Problems and Solutions |
|
|
183 | (1) |
|
Clicking a Page Button Manages the Wrong Data Type |
|
|
183 | (1) |
|
Clicking a Page Button Has No Effect |
|
|
184 | (1) |
|
The "Cannot Insert Explicit Value for Identity Column" Exception |
|
|
184 | (1) |
|
|
184 | (1) |
|
Session Objects Are Lost or Inconsistently Available |
|
|
184 | (1) |
|
|
184 | (1) |
|
Chapter 10 SportsStore: Creating a RESTful Web Service |
|
|
185 | (18) |
|
Preparing for This Chapter |
|
|
185 | (1) |
|
|
186 | (12) |
|
|
186 | (2) |
|
Creating the API Controller |
|
|
188 | (2) |
|
|
190 | (1) |
|
Projecting a Result to Exclude Null Navigation Properties |
|
|
190 | (2) |
|
Including Related Data in a Web Service Response |
|
|
192 | (3) |
|
Querying for Multiple Objects |
|
|
195 | (3) |
|
Completing the Web Service |
|
|
198 | (4) |
|
|
200 | (2) |
|
Common Problems and Solutions |
|
|
202 | (1) |
|
Null Property Values When Storing or Updating Objects |
|
|
202 | (1) |
|
Slow Web Service Requests |
|
|
202 | (1) |
|
The "Cannot Insert Explicit Value for Identity Column" Exception |
|
|
202 | (1) |
|
|
202 | (1) |
Part II: Entity Framework Core 2 in Detail |
|
203 | (260) |
|
Chapter 11 Working with Entity Framework Core |
|
|
205 | (34) |
|
Creating the ASRNET Core MVC Project |
|
|
206 | (7) |
|
Creating the Data Model Class |
|
|
207 | (1) |
|
Configuring Services and Middleware |
|
|
208 | (1) |
|
Adding a Controller and View |
|
|
209 | (1) |
|
Adding the Bootstrap CSS Framework |
|
|
210 | (1) |
|
Configuring the HTTP Port |
|
|
211 | (1) |
|
Running the Example Application |
|
|
212 | (1) |
|
Adding and Configuring Entity Framework Core |
|
|
213 | (10) |
|
|
213 | (1) |
|
Creating the Database Context Class |
|
|
214 | (1) |
|
Preparing the Entity Class |
|
|
214 | (1) |
|
|
215 | (1) |
|
Configuring the Database Provider |
|
|
216 | (3) |
|
Configuring Entity Framework Core Logging |
|
|
219 | (4) |
|
Implementing the Repository Pattern |
|
|
223 | (9) |
|
Defining the Repository Interface and Implementation Class |
|
|
224 | (2) |
|
Avoiding the lEnumerable vs IQueryable Pitfall |
|
|
226 | (4) |
|
Hiding the Data Operations |
|
|
230 | (2) |
|
Completing the Example MVC Application |
|
|
232 | (5) |
|
Completing the Repository |
|
|
233 | (1) |
|
Adding the Action Methods |
|
|
234 | (1) |
|
Updating and Adding Views |
|
|
235 | (2) |
|
|
237 | (2) |
|
Chapter 12 Performing Data Operations |
|
|
239 | (26) |
|
Preparing for This Chapter |
|
|
240 | (2) |
|
Starting the Example Application |
|
|
241 | (1) |
|
|
242 | (10) |
|
|
242 | (4) |
|
|
246 | (1) |
|
Querying for Specific Objects |
|
|
247 | (5) |
|
|
252 | (2) |
|
Understanding Key Assignment |
|
|
253 | (1) |
|
|
254 | (8) |
|
Updating a Complete Object |
|
|
254 | (2) |
|
Querying the Existing Data Before Updating |
|
|
256 | (2) |
|
Updating in a Single Database Operation |
|
|
258 | (4) |
|
|
262 | (2) |
|
|
264 | (1) |
|
Chapter 13 Understanding Migrations |
|
|
265 | (34) |
|
Preparing for This Chapter |
|
|
266 | (1) |
|
|
267 | (7) |
|
Working with the Initial Migration |
|
|
267 | (4) |
|
Examining the Migration SQL |
|
|
271 | (1) |
|
|
272 | (1) |
|
Seeding the Database and Running the Application |
|
|
273 | (1) |
|
Creating Additional Migrations |
|
|
274 | (3) |
|
Adding Another Property to the Data Model |
|
|
276 | (1) |
|
|
277 | (4) |
|
|
278 | (1) |
|
|
278 | (1) |
|
Updating to a Specific Migration |
|
|
279 | (1) |
|
|
280 | (1) |
|
|
281 | (1) |
|
Working with Multiple Databases |
|
|
281 | (5) |
|
|
282 | (1) |
|
Configuring the Application |
|
|
283 | (2) |
|
Creating and Applying Migrations |
|
|
285 | (1) |
|
Managing Migrations Programmatically |
|
|
286 | (6) |
|
Creating the Migration Manager Class |
|
|
286 | (3) |
|
Creating the Migrations Controller and View |
|
|
289 | (2) |
|
Configuring the Application |
|
|
291 | (1) |
|
Running the Migrations Manager |
|
|
291 | (1) |
|
Seeding Databases Programmatically |
|
|
292 | (6) |
|
|
294 | (2) |
|
|
296 | (2) |
|
|
298 | (1) |
|
Chapter 14 Creating Data Relationships |
|
|
299 | (34) |
|
Preparing for This Chapter |
|
|
300 | (1) |
|
|
300 | (12) |
|
Adding a Navigation Property |
|
|
301 | (1) |
|
|
302 | (2) |
|
Querying and Displaying the Related Data |
|
|
304 | (4) |
|
Updating the View to Display Related Data |
|
|
308 | (2) |
|
|
310 | (2) |
|
Creating and Updating Related Data |
|
|
312 | (6) |
|
Creating a New Supplier When Creating a New Product |
|
|
314 | (2) |
|
Updating a Supplier when Updating a Product |
|
|
316 | (2) |
|
|
318 | (3) |
|
Creating a Required Relationship |
|
|
321 | (6) |
|
Creating a Foreign Key Property |
|
|
321 | (2) |
|
Dropping the Database and Preparing the Seed Data |
|
|
323 | (1) |
|
Updating and Seeding the Database |
|
|
324 | (1) |
|
Understanding the Required Relationship Delete Operation |
|
|
325 | (2) |
|
Querying for Multiple Relationships |
|
|
327 | (4) |
|
Updating and Seeding the Database |
|
|
328 | (1) |
|
Querying the Chain of Navigation Properties |
|
|
329 | (2) |
|
|
331 | (2) |
|
Chapter 15 Working with Relationships Part 1 |
|
|
333 | (38) |
|
Preparing for This Chapter |
|
|
333 | (1) |
|
Directly Accessing Related Data |
|
|
334 | (9) |
|
|
335 | (3) |
|
Accessing Related Data Using a Type Parameter |
|
|
338 | (5) |
|
Completing a Data Relationship |
|
|
343 | (10) |
|
Querying Related Data in a One-to-Many Relationship |
|
|
344 | (9) |
|
Working with Related Data in a One-to-Many Relationship |
|
|
353 | (16) |
|
|
355 | (3) |
|
Creating New Related Objects |
|
|
358 | (4) |
|
|
362 | (7) |
|
|
369 | (2) |
|
Chapter 16 Working with Relationship Part 2 |
|
|
371 | (32) |
|
Preparing for This Chapter |
|
|
371 | (1) |
|
Completing a One-to-One Relationship |
|
|
372 | (3) |
|
Defining the Navigation Property |
|
|
372 | (1) |
|
Selecting the Dependent Entity Class |
|
|
373 | (1) |
|
Creating and Applying the Migration |
|
|
374 | (1) |
|
Working with One-to-One Relationships |
|
|
375 | (14) |
|
Querying Related Data in a One-to-One Relationship |
|
|
375 | (2) |
|
Creating and Updating Related Objects |
|
|
377 | (3) |
|
Changing One-to-One Relationships |
|
|
380 | (9) |
|
Defining Many-to-Many Relationships |
|
|
389 | (13) |
|
Creating the Junction Class |
|
|
390 | (1) |
|
Completing the Many-to-Many Relationship |
|
|
391 | (1) |
|
Preparing the Application |
|
|
392 | (2) |
|
Querying for Many-to-Many Data |
|
|
394 | (3) |
|
Managing Many-to-Many Relationships |
|
|
397 | (5) |
|
|
402 | (1) |
|
Chapter 17 Scaffolding an Existing Database |
|
|
403 | (28) |
|
Preparing for This Chapter |
|
|
404 | (12) |
|
Understanding the Example Existing Database |
|
|
404 | (1) |
|
Connecting to the Database Server |
|
|
405 | (1) |
|
|
405 | (6) |
|
Creating the ASRNET Core MVC Project |
|
|
411 | (4) |
|
Testing the Example Application |
|
|
415 | (1) |
|
Scaffolding an Existing Database |
|
|
416 | (7) |
|
Performing the Scaffold Process |
|
|
416 | (3) |
|
Using a Scaffolded Data Model in the ASRNET Core MVC |
|
|
419 | (4) |
|
Responding to Database Changes |
|
|
423 | (5) |
|
|
424 | (1) |
|
|
425 | (1) |
|
Updating the Context Class |
|
|
426 | (1) |
|
Updating the Controllers and Views |
|
|
426 | (2) |
|
Adding Persistent Data Model Features |
|
|
428 | (2) |
|
|
430 | (1) |
|
Chapter 18 Manually Modeling a Database |
|
|
431 | (32) |
|
Preparing for This Chapter |
|
|
432 | (1) |
|
Creating a Manual Data Model |
|
|
432 | (19) |
|
Creating the Context and Entity Classes |
|
|
433 | (1) |
|
Creating the Controller and View |
|
|
434 | (2) |
|
Understanding the Basic Data Model Conventions |
|
|
436 | (1) |
|
Overriding the Data Model Conventions |
|
|
437 | (6) |
|
|
443 | (6) |
|
Completing the Data Model |
|
|
449 | (2) |
|
Using the Manually Created Data Model |
|
|
451 | (10) |
|
Querying Data in a Manually Created Data Model |
|
|
452 | (3) |
|
Updating Data in a Manually Created Data Model |
|
|
455 | (6) |
|
|
461 | (2) |
Part III: Advanced Entity Framework Core 2 |
|
463 | (184) |
|
Chapter 19 Working with Keys |
|
|
465 | (32) |
|
Preparing for This Chapter |
|
|
466 | (9) |
|
|
467 | (1) |
|
Creating the Controller and Views |
|
|
468 | (3) |
|
Configuring the Application |
|
|
471 | (3) |
|
Creating the Database and Testing the Application |
|
|
474 | (1) |
|
|
475 | (4) |
|
Understanding the Identity Strategy |
|
|
475 | (1) |
|
Understanding the Hi-Lo Key Strategy |
|
|
476 | (3) |
|
Working with Natural Keys |
|
|
479 | (8) |
|
Ensuring Unique Values for Natural Keys |
|
|
480 | (2) |
|
Creating an Alternate Key |
|
|
482 | (5) |
|
Using Natural Keys as Primary Keys |
|
|
487 | (3) |
|
|
490 | (5) |
|
|
495 | (2) |
|
|
497 | (30) |
|
Preparing for This Chapter |
|
|
498 | (2) |
|
Managing Change Tracking for Query Results |
|
|
500 | (5) |
|
Removing Individual Objects from Change Tracking |
|
|
502 | (1) |
|
Changing the Default Change Tracking Behavior |
|
|
503 | (2) |
|
|
505 | (7) |
|
Overriding a Query Filter |
|
|
509 | (3) |
|
Querying Using Search Patterns |
|
|
512 | (3) |
|
Making Asynchronous Queries |
|
|
515 | (3) |
|
Explicitly Compiling Queries |
|
|
518 | (2) |
|
Avoiding the Client Evaluation Pitfall |
|
|
520 | (5) |
|
Throwing a Client Evaluation Exception |
|
|
523 | (2) |
|
|
525 | (2) |
|
|
527 | (36) |
|
Preparing for This Chapter |
|
|
528 | (3) |
|
Specifying SQL Data Types |
|
|
531 | (5) |
|
Specifying a Maximum Length |
|
|
533 | (1) |
|
|
534 | (2) |
|
Validating or Formatting Data Values |
|
|
536 | (7) |
|
Avoiding the Backing Field Selective Update Pitfall |
|
|
539 | (4) |
|
Hiding Data Values from the MVC Application |
|
|
543 | (3) |
|
Accessing Shadow Property Values |
|
|
544 | (1) |
|
Including Shadow Properties in Queries |
|
|
545 | (1) |
|
|
546 | (5) |
|
Displaying the Default Value |
|
|
548 | (3) |
|
Detecting Concurrent Updates |
|
|
551 | (11) |
|
|
552 | (5) |
|
Using a Row Version to Detect Concurrent Updates |
|
|
557 | (5) |
|
|
562 | (1) |
|
|
563 | (26) |
|
Preparing for This Chapter |
|
|
563 | (5) |
|
Understanding Delete Constraints |
|
|
568 | (3) |
|
Configuring the Delete Behavior |
|
|
570 | (1) |
|
Using the Cascade Delete Behavior |
|
|
571 | (2) |
|
Setting Foreign Keys to Null |
|
|
573 | (8) |
|
Relying on the Database Server to Change Foreign Keys |
|
|
574 | (2) |
|
Relying on Entity Framework Core to Update Foreign Keys |
|
|
576 | (5) |
|
Taking Control of the Delete Operation |
|
|
581 | (2) |
|
Re-creating the Cascade Behavior |
|
|
581 | (1) |
|
Re-creating the Set Null Behavior |
|
|
582 | (1) |
|
Reinstating the Soft-Delete Feature |
|
|
583 | (4) |
|
|
587 | (2) |
|
Chapter 23 Using Database Server Features |
|
|
589 | (40) |
|
Preparing for This Chapter |
|
|
590 | (4) |
|
|
594 | (17) |
|
|
594 | (13) |
|
Calling Stored Procedures or Other Operations |
|
|
607 | (4) |
|
Using Server-Generated Values |
|
|
611 | (17) |
|
Using Database Server-Generated Default Values |
|
|
611 | (5) |
|
Incorporating Sequential Values |
|
|
616 | (3) |
|
Computing Values in the Database |
|
|
619 | (4) |
|
Modeling Automatically Generated Values |
|
|
623 | (5) |
|
|
628 | (1) |
|
Chapter 24 Using Transactions |
|
|
629 | (18) |
|
Preparing for This Chapter |
|
|
630 | (3) |
|
Understanding the Default Behavior |
|
|
633 | (5) |
|
Performing Independent Changes |
|
|
634 | (2) |
|
Disabling Automatic Transactions |
|
|
636 | (2) |
|
Using Explicit Transactions |
|
|
638 | (7) |
|
Including Other Operations in a Transaction |
|
|
640 | (2) |
|
Changing the Transaction Isolation Level |
|
|
642 | (3) |
|
|
645 | (2) |
Index |
|
647 | |