Skip to content

Commit 0401c6d

Browse files
pgoldman5699tdykstra
authored andcommitted
Updated code & screenshots for /aspnetcore/data/ef-mvc/ tutorials (dotnet#10802)
* screenshots * ASP.NET MVC tutorial 2.2 * updates * Minor updates * minor edits * minor edits * minor edits * minor edits * minor edits * Shorter title * Shortened title * Shortened title * Shortened title * Shortened title * Shortened title * Shortened title * Shortened title * Shortened title * Shortened title * Acknowledgements- John Parente and me * Better acknowledgements
1 parent 33dedd6 commit 0401c6d

16 files changed

+507
-310
lines changed

aspnetcore/data/ef-mvc/advanced.md

+60-32
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
---
2-
title: ASP.NET Core MVC with EF Core - Advanced - 10 of 10
2+
title: "Tutorial: Learn about advanced scenarios - ASP.NET MVC with EF Core"
3+
description: "This tutorial introduces useful topics for going beyond the basics of developing ASP.NET Core web apps that use Entity Framework Core."
34
author: rick-anderson
4-
description: This tutorial introduces useful topics for going beyond the basics of developing ASP.NET Core web apps that use Entity Framework Core.
55
ms.author: tdykstra
66
ms.custom: mvc
7-
ms.date: 10/24/2018
7+
ms.date: 02/05/2019
8+
ms.topic: tutorial
89
uid: data/ef-mvc/advanced
910
---
1011

11-
# ASP.NET Core MVC with EF Core - Advanced - 10 of 10
12+
# Tutorial: Learn about advanced scenarios - ASP.NET MVC with EF Core
1213

13-
[!INCLUDE [RP better than MVC](~/includes/RP-EF/rp-over-mvc-21.md)]
14+
In the previous tutorial, you implemented table-per-hierarchy inheritance. This tutorial introduces several topics that are useful to be aware of when you go beyond the basics of developing ASP.NET Core web applications that use Entity Framework Core.
1415

15-
::: moniker range="= aspnetcore-2.0"
16+
In this tutorial, you:
1617

17-
By [Tom Dykstra](https://github.com/tdykstra) and [Rick Anderson](https://twitter.com/RickAndMSFT)
18+
> [!div class="checklist"]
19+
> * Perform raw SQL queries
20+
> * Call a query to return entities
21+
> * Call a query to return other types
22+
> * Call an update query
23+
> * Examine SQL queries
24+
> * Create an abstraction layer
25+
> * Learn about Automatic change detection
26+
> * Learn about EF Core source code and development plans
27+
> * Learn how to use dynamic LINQ to simplify code
1828
19-
The Contoso University sample web application demonstrates how to create ASP.NET Core MVC web applications using Entity Framework Core and Visual Studio. For information about the tutorial series, see [the first tutorial in the series](intro.md).
29+
## Prerequisites
2030

21-
In the previous tutorial, you implemented table-per-hierarchy inheritance. This tutorial introduces several topics that are useful to be aware of when you go beyond the basics of developing ASP.NET Core web applications that use Entity Framework Core.
31+
* [Implement Inheritance with EF Core in an ASP.NET Core MVC web app](inheritance.md)
2232

23-
## Raw SQL Queries
33+
## Perform raw SQL queries
2434

2535
One of the advantages of using the Entity Framework is that it avoids tying your code too closely to a particular method of storing data. It does this by generating SQL queries and commands for you, which also frees you from having to write them yourself. But there are exceptional scenarios when you need to run specific SQL queries that you have manually created. For these scenarios, the Entity Framework Code First API includes methods that enable you to pass SQL commands directly to the database. You have the following options in EF Core 1.0:
2636

@@ -32,7 +42,7 @@ If you need to run a query that returns types that aren't entities, you can use
3242

3343
As is always true when you execute SQL commands in a web application, you must take precautions to protect your site against SQL injection attacks. One way to do that is to use parameterized queries to make sure that strings submitted by a web page can't be interpreted as SQL commands. In this tutorial you'll use parameterized queries when integrating user input into a query.
3444

35-
## Call a query that returns entities
45+
## Call a query to return entities
3646

3747
The `DbSet<TEntity>` class provides a method that you can use to execute a query that returns an entity of type `TEntity`. To see how this works you'll change the code in the `Details` method of the Department controller.
3848

@@ -44,7 +54,7 @@ To verify that the new code works correctly, select the **Departments** tab and
4454

4555
![Department Details](advanced/_static/department-details.png)
4656

47-
## Call a query that returns other types
57+
## Call a query to return other types
4858

4959
Earlier you created a student statistics grid for the About page that showed the number of students for each enrollment date. You got the data from the Students entity set (`_context.Students`) and used LINQ to project the results into a list of `EnrollmentDateGroup` view model objects. Suppose you want to write the SQL itself rather than using LINQ. To do that you need to run a SQL query that returns something other than entity objects. In EF Core 1.0, one way to do that is write ADO.NET code and get the database connection from EF.
5060

@@ -78,7 +88,7 @@ When the **Update** button is clicked, the HttpPost method is called, and multip
7888

7989
In **Solution Explorer**, right-click the *Views/Courses* folder, and then click **Add > New Item**.
8090

81-
In the **Add New Item** dialog, click **ASP.NET** under **Installed** in the left pane, click **MVC View Page**, and name the new view *UpdateCourseCredits.cshtml*.
91+
In the **Add New Item** dialog, click **ASP.NET Core** under **Installed** in the left pane, click **Razor View**, and name the new view *UpdateCourseCredits.cshtml*.
8292

8393
In *Views/Courses/UpdateCourseCredits.cshtml*, replace the template code with the following code:
8494

@@ -98,7 +108,7 @@ Note that production code would ensure that updates always result in valid data.
98108

99109
For more information about raw SQL queries, see [Raw SQL Queries](/ef/core/querying/raw-sql).
100110

101-
## Examine SQL sent to the database
111+
## Examine SQL queries
102112

103113
Sometimes it's helpful to be able to see the actual SQL queries that are sent to the database. Built-in logging functionality for ASP.NET Core is automatically used by EF Core to write logs that contain the SQL for queries and updates. In this section you'll see some examples of SQL logging.
104114

@@ -134,7 +144,7 @@ You'll notice something here that might surprise you: the SQL selects up to 2 ro
134144

135145
Note that you don't have to use debug mode and stop at a breakpoint to get logging output in the **Output** window. It's just a convenient way to stop the logging at the point you want to look at the output. If you don't do that, logging continues and you have to scroll back to find the parts you're interested in.
136146

137-
## Repository and unit of work patterns
147+
## Create an abstraction layer
138148

139149
Many developers write code to implement the repository and unit of work patterns as a wrapper around code that works with the Entity Framework. These patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD). However, writing additional code to implement these patterns isn't always the best choice for applications that use EF, for several reasons:
140150

@@ -164,7 +174,7 @@ If you're tracking a large number of entities and you call one of these methods
164174
_context.ChangeTracker.AutoDetectChangesEnabled = false;
165175
```
166176

167-
## Entity Framework Core source code and development plans
177+
## EF Core source code and development plans
168178

169179
The Entity Framework Core source is at [https://github.com/aspnet/EntityFrameworkCore](https://github.com/aspnet/EntityFrameworkCore). The EF Core repository contains nightly builds, issue tracking, feature specs, design meeting notes, and [the roadmap for future development](https://github.com/aspnet/EntityFrameworkCore/wiki/Roadmap). You can file or find bugs, and contribute.
170180

@@ -175,27 +185,19 @@ Although the source code is open, Entity Framework Core is fully supported as a
175185
To reverse engineer a data model including entity classes from an existing database, use the [scaffold-dbcontext](/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext) command. See the [getting-started tutorial](/ef/core/get-started/aspnetcore/existing-db).
176186

177187
<a id="dynamic-linq"></a>
178-
## Use dynamic LINQ to simplify sort selection code
188+
189+
## Use dynamic LINQ to simplify code
179190

180191
The [third tutorial in this series](sort-filter-page.md) shows how to write LINQ code by hard-coding column names in a `switch` statement. With two columns to choose from, this works fine, but if you have many columns the code could get verbose. To solve that problem, you can use the `EF.Property` method to specify the name of the property as a string. To try out this approach, replace the `Index` method in the `StudentsController` with the following code.
181192

182193
[!code-csharp[](intro/samples/cu/Controllers/StudentsController.cs?name=snippet_DynamicLinq)]
183194

184-
## Next steps
185-
186-
This completes this series of tutorials on using the Entity Framework Core in an ASP.NET Core MVC application.
187-
188-
For more information about EF Core, see the [Entity Framework Core documentation](/ef/core). A book is also available: [Entity Framework Core in Action](https://www.manning.com/books/entity-framework-core-in-action).
189-
190-
For information on how to deploy a web app, see <xref:host-and-deploy/index>.
191-
192-
For information about other topics related to ASP.NET Core MVC, such as authentication and authorization, see <xref:index>.
193-
194195
## Acknowledgments
195196

196-
Tom Dykstra and Rick Anderson (twitter @RickAndMSFT) wrote this tutorial. Rowan Miller, Diego Vega, and other members of the Entity Framework team assisted with code reviews and helped debug issues that arose while we were writing code for the tutorials.
197+
Tom Dykstra and Rick Anderson (twitter @RickAndMSFT) wrote this tutorial. Rowan Miller, Diego Vega, and other members of the Entity Framework team assisted with code reviews and helped debug issues that arose while we were writing code for the tutorials. John Parente and Paul Goldman worked on updating the tutorial for ASP.NET Core 2.2.
197198

198-
## Common errors
199+
<a id="common-errors"></a>
200+
## Troubleshoot common errors
199201

200202
### ContosoUniversity.dll used by another process
201203

@@ -241,7 +243,33 @@ Solution:
241243

242244
Check the connection string. If you have manually deleted the database file, change the name of the database in the construction string to start over with a new database.
243245

244-
::: moniker-end
246+
## Get the code
247+
248+
[Download or view the completed application.](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-mvc/intro/samples/cu-final)
249+
250+
## Additional resources
251+
252+
For more information about EF Core, see the [Entity Framework Core documentation](/ef/core). A book is also available: [Entity Framework Core in Action](https://www.manning.com/books/entity-framework-core-in-action).
253+
254+
For information on how to deploy a web app, see <xref:host-and-deploy/index>.
255+
256+
For information about other topics related to ASP.NET Core MVC, such as authentication and authorization, see <xref:index>.
257+
258+
## Next steps
245259

246-
> [!div class="step-by-step"]
247-
> [Previous](inheritance.md)
260+
In this tutorial, you:
261+
262+
> [!div class="checklist"]
263+
> * Performed raw SQL queries
264+
> * Called a query to return entities
265+
> * Called a query to return other types
266+
> * Called an update query
267+
> * Examined SQL queries
268+
> * Created an abstraction layer
269+
> * Learned about Automatic change detection
270+
> * Learned about EF Core source code and development plans
271+
> * Learned how to use dynamic LINQ to simplify code
272+
273+
This completes this series of tutorials on using the Entity Framework Core in an ASP.NET Core MVC application. If you want to learn about using EF 6 with ASP.NET Core, see the next article.
274+
> [!div class="nextstepaction"]
275+
> [EF 6 with ASP.NET Core](../entity-framework-6.md)

0 commit comments

Comments
 (0)