From the course: Learning ASP.NET Core MVC

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Write to a database using Entity Framework

Write to a database using Entity Framework

From the course: Learning ASP.NET Core MVC

Start my 1-month free trial

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…

Contents