ASP.NET Core Web Apps with EF Core [3 of 5] | Entity Framework Core for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] >> Hi, friends. Welcome back to Entity Framework Core for Beginners. In this video, I'm going to show you how you can use Entity Framework Core, along with ASP.NET Core, to streamline your web development. I've already created an empty ASP.NET Core Razor Pages web app. I used the command I showed you in the last video to scaffold a DB context and models against that same database. Now we're going to scaffold some razor pages that use ContosoPizzaContext to interact with the database. ContosoPizzaContext contains the connection string that was used for scaffolding in the OnConfiguring method. We're going to get our connection information from configuration, so let's delete that method. Now I'm going to visit program.cs. I'll paste in some code, and then I'll add the using directives to resolve the references. The AddDbContext extension method registers ContosoPizzaContext with ASP.NET Core's dependency injection container. We pass the method a Lambda expression that configures ef code to use the SQL Server database provider using a connection string retrieved from configuration. In previous videos, I've mentioned that it's bad practice to store your connection strings with your code. The.NET secrets manager, gives us a mechanism to separate our secrets from our code. Let's use the.NET secrets manager to store that connection string. I'm right clicking on the project and selecting "Manage User Secrets." Visual Studio opens a file called secrets.json for editing, and I'll paste in my connection string. This file is stored in your user profile on your development machine. The secrets aren't encrypted. It's just a location to store them away from your code. At runtime, ASP.NET Core will look for the configuration in-app settings.json and other locations. Secrets.json is one of the locations it checks. If you're using the.NET CLI, you must first initialize the secret store with.NET user secrets in it. Then you can add your secrets with.NET user secrets set. Now we're going to generate razor pages to support creating, reading, updating, and deleting products. These operations are collectively referred to as CRUD. The first thing I'm going to do is add the Microsoft.Visual Studio.Web.CodeGeneration.Design package to the project. This installs some dependencies for the scaffolding tool. Now I'll create a products folder inside the pages folder. After that, I'll "Right-click" on the folder, select "Add," and then "New Scaffolded Item." In the dialog that follows, select "Razor Pages" using entity framework CRUD. Select "Product" as the model class, and "ContosoPizzaContext" as the data context class. Click "Add." If you're using the.NET CLI, first install the.NET ASP.NET code generator tool as a global tool. Then use the.NET ASP.NET code generator command to scaffold the pages. I've put the full command in the notes. The scaffolding creates five pages; Create, delete, details, edit, and index. I'm going to run the application. I'll edit my URL to navigate directly to the product's index page. This view lists all the products in the table. I'm going to leave the app running, but I'll switch back to my Ide. Let's look at the code for index.cshtml.cs. Notice that we're injecting ContosoPizzacontext into the constructor. ASP.NET Core's dependency injection container takes care of this for us. All we have to do is ensure the constructor has the right signature. The product's collection is accessed by a property on the page model class. The onGet method sets that property equal to the contents of the product's DB set. The Razor view enumerates over that list of products and for each product lists the name and price. Let's use the create page to create a new product. There's a new product. Let's look at what happened in the code. We'll start with the page model. The onGet method in the page model returns the empty form, which is a Razor view. The Razor view has elements to support name and price. It uses label, input, and span elements to build the form that we saw earlier. This includes validation that enforces the constraints in our model classes. When we post the form, the model binder binds the elements on the form to our product property, which in turn is added to the product's DB set. Finally, we call save changes async to save the changes to the database. Let's edit a record. When we visit this page, the name and price elements are pre-populated with existing data. Looking at the page model for edit, the onGet method queries the database for products that match the ID that was passed in on the URL's query string. We retrieve that product and present it for the user to edit. When the user posts the form, the model binder builds a product object from the elements on the page, attaches it to the existing entity, marks it as modified, and saves the changes. The final view we're going to look at is delete. The Delete view is similar to the Edit view. The first thing onGet does is look up the product by ID and display it for the user. When the user clicks the "Submit" button, we find that same product by its ID. Then we call remove on the product's DB set, passing in the entity to delete. In this video, we saw how easy it is to use ASP.NET Core, along with Entity Framework Core, to streamline your web development. In the next video, we're going to look at using different database providers with Entity Framework Core. [MUSIC]
Info
Channel: dotnet
Views: 31,955
Rating: undefined out of 5
Keywords: .NET
Id: c-wN-fc594c
Channel Id: undefined
Length: 8min 47sec (527 seconds)
Published: Tue Apr 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.