You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardexpand all lines: aspnetcore/data/ef-mvc/advanced.md
+60-32
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,36 @@
1
1
---
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."
3
4
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.
5
5
ms.author: tdykstra
6
6
ms.custom: mvc
7
-
ms.date: 10/24/2018
7
+
ms.date: 02/05/2019
8
+
ms.topic: tutorial
8
9
uid: data/ef-mvc/advanced
9
10
---
10
11
11
-
# ASP.NET Core MVC with EF Core - Advanced - 10 of 10
12
+
# Tutorial: Learn about advanced scenarios - ASP.NET MVC with EF Core
12
13
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.
14
15
15
-
::: moniker range="= aspnetcore-2.0"
16
+
In this tutorial, you:
16
17
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
18
28
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
20
30
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)
22
32
23
-
## Raw SQL Queries
33
+
## Perform raw SQL queries
24
34
25
35
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:
26
36
@@ -32,7 +42,7 @@ If you need to run a query that returns types that aren't entities, you can use
32
42
33
43
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.
34
44
35
-
## Call a query that returns entities
45
+
## Call a query to return entities
36
46
37
47
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.
38
48
@@ -44,7 +54,7 @@ To verify that the new code works correctly, select the **Departments** tab and
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.
50
60
@@ -78,7 +88,7 @@ When the **Update** button is clicked, the HttpPost method is called, and multip
78
88
79
89
In **Solution Explorer**, right-click the *Views/Courses* folder, and then click **Add > New Item**.
80
90
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*.
82
92
83
93
In *Views/Courses/UpdateCourseCredits.cshtml*, replace the template code with the following code:
84
94
@@ -98,7 +108,7 @@ Note that production code would ensure that updates always result in valid data.
98
108
99
109
For more information about raw SQL queries, see [Raw SQL Queries](/ef/core/querying/raw-sql).
100
110
101
-
## Examine SQL sent to the database
111
+
## Examine SQL queries
102
112
103
113
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.
104
114
@@ -134,7 +144,7 @@ You'll notice something here that might surprise you: the SQL selects up to 2 ro
134
144
135
145
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.
136
146
137
-
## Repository and unit of work patterns
147
+
## Create an abstraction layer
138
148
139
149
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:
140
150
@@ -164,7 +174,7 @@ If you're tracking a large number of entities and you call one of these methods
## Entity Framework Core source code and development plans
177
+
## EF Core source code and development plans
168
178
169
179
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.
170
180
@@ -175,27 +185,19 @@ Although the source code is open, Entity Framework Core is fully supported as a
175
185
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).
176
186
177
187
<aid="dynamic-linq"></a>
178
-
## Use dynamic LINQ to simplify sort selection code
188
+
189
+
## Use dynamic LINQ to simplify code
179
190
180
191
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.
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
-
194
195
## Acknowledgments
195
196
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.
197
198
198
-
## Common errors
199
+
<aid="common-errors"></a>
200
+
## Troubleshoot common errors
199
201
200
202
### ContosoUniversity.dll used by another process
201
203
@@ -241,7 +243,33 @@ Solution:
241
243
242
244
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.
243
245
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
245
259
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