Learn how to add an MVC controller that uses EF to query and save data. Find out that the controller takes a context as a constructor parameter. ASP.NET dependency injection will take care of passing an instance of context into the controller. The controller contains an Index action, which displays all actors in the database, and a create action, which inserts a new actor into the database.
- [Instructor] So we're now ready to create our controller.…So let's go ahead and navigate to…the controllers folder in our project.…And right click, select add, choose class,…and let's call this actors controller.…We'll hit enter, and the first thing…we'll want to do is to inherit…from the controller class.…Let's go ahead and do that.…And bring in the namespace…by holding the control key and period.…
Specifically AspNetCore.Mvc namespace…and let's start off with creating a constructor.…One of my favorite snippets, ctor,…hit tab, it creates a constructor.…And we'll start off by setting a private variable…for our context, and the type will be ActorDbContext.…
It'll bring in the namespace, control period,…and we'll call this _context.…And that context will be passed into our constructor.…And here we'll assign the value…passed in to our private variable.…Okay, looks good.…Next thing, we'll create an index method.…We're going to type, we'll have IActionResult.…And simply return a view with a list of actors…and the type will be a list.…
- Entity Framework (EF) overview
- Uses for EF
- Installing EF
- Creating entities
- Creating the DbContext
- Performing migrations
- Creating an MVC application
- Installing EF packages
- Reverse engineering the model
- Performing context and dependency injection
- Creating the controller and the view
- Using SqlServerTypeMapper
Skill Level Intermediate
Q: This course was updated on 06/23/2017. What changed?
A: The following topics were updated: using the exercise files; Mac install prerequisites; Entity Framework on Mac, part 1; and Entity Framework on Mac, part 2.