From the course: Learning ASP.NET Core MVC
Unlock the full course today
Join today to access over 23,200 courses taught by industry experts.
Write to a database using Entity Framework
From the course: Learning ASP.NET Core MVC
Write to a database using Entity Framework
- [Instructor] Now that we've created and configured our new data context class, I'll show you how to put this new class to good use by storing and retrieving the blog post for our demo application. Since we've already done all of the dirty work of creating and configuring the data context class, all that's left is to simply inject it into our controllers. So, let's open up our blog controller again and start by injecting a instance of the blog data context class into the constructor. Now that we have that, I can add the following lines to the create action to save the newly submitted post to the database. Notice how saving the post to the database is a two step process. Entity Framework works on the Unit of Work pattern, which is really just a fancy way of saying that first you tell the data context everything that you want to do. For example, adding a new post to the database. Then, the second step is to tell the data context to actually go ahead and execute what you've asked for…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Create strongly typed HTML forms4m 5s
-
(Locked)
Post form data to controller actions4m 9s
-
(Locked)
Validate form post data5m 55s
-
(Locked)
Use tag helpers to simplify forms5m 7s
-
(Locked)
Represent the database using Entity Framework6m 9s
-
(Locked)
Write to a database using Entity Framework1m 35s
-
(Locked)
Read from a database using Entity Framework2m 26s
-
-
-
-