📓 3.3.1.7 Create with EF Core
In this lesson, we'll add more CRUD functionality to our To Do List using our new EF Core-powered ORM capabilities. Specifically, we'll focus on the create action here.
Create
Creating a new item is a two-step process:
- We need a form to supply information about the new item.
- We also need functionality to submit that form and save the information it contains to our database.
This means we'll also need two Create
actions:
- A
GET
action to display our form to users. - A
POST
action to manage form submission.
Controller
Let's add these actions to our ItemsController
now:
...
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Item item)
{
_db.Items.Add(item);
_db.SaveChanges();
return RedirectToAction("Index");
}
...
The
Create()
GET
route is used exactly the same way asNew()
was before we started using Entity. You may notice that this strays from our RESTful routing practices — previously, we learned thatNew()
should be used for thisGET
request. We are now straying from our RESTful routing practices in order to make use of new tools that streamline our forms: HTML helper methods. We will go into depth about HTML helper methods in the next lesson.The second action is our
POST
request. This route will take anitem
as an argument, add it to theItems
DbSet
, and then save the changes to our database object. Afterwards, it will redirect users to theIndex
view.
Add()
is a DbSet
method we run on our DBSet
property of our ToDoListContext
, while SaveChanges()
is a DbContext
method that we run on the ToDoListContext
itself (which extends the DbContext
class).
Together, they update the DBSet
and then sync those changes to the database which the ToDoListContext
represents. Once again, EF Core takes care of the work for us.
In the next lesson, we'll learn about HTML helper methods, which will make it much easier to create forms and simplify our HTML.