Learn how to migrate the Movie model class from MVC 5 to Core MVC, and split it into two model classes. This refines the application data structure updates the sample application to a more realistic object graph.
- [Instructor] Now it's time to migrate the data access over to .net core. So we're going to start by taking the movie model and moving into .net core. The initial sample is very basic, there's only the one class. So we're going to do a couple things to make it a little more advanced. We're going to take advantage of concurrency, which the original sample does not do. As well as add a movie genre class, so we can show navigation properties.
So here's our MVC 5 application. Just go ahead and copy the entire movie class. And we'll switch over to our .net core. And let's add a new folder, call it Models in the Data Access Layer project. And we're going to add a new folder under here, and call it Base. And we'll get to that shortly. Add a class, called Movie.cs.
And we'll paste in the code from the MVC 5 app. Now we have to add some usings here. So we'll take advantage of visual studio refactorings. And we could stop here, and successfully migrate, but we also want to do a little bit of improvement, so we're going to create an entity base class. And this base class, this is pretty standard with how I do EF work, whether it's EF 6 or Core.
We're going to add in ID field, as well as a timestamp. And a timestamp is a concurrency field and it is represented in .net as a byter write, technically of length eight, but we don't need to worry about that. And we'll decorate it with the timestamp attribute. We'll have to add a using for that. And then the ID which is the primary key we'll go ahead and call that out specifically.
We don't need to do this because any current conventions looks at any field name ID or class name ID and automatically makes it to key. And then we'll also want to tell any framework that it is a sequence. Pass in a database generated option . identity. And we'll add the appropriate usings for that. And now we have our base class. So we go back to the movie, we can delete the ID because we already have that.
And then we want to add in a genre. So we can just start typing it right here. Public class movie genre, and that's also going to be on the entity base. And it's going to have two properties in addition to the ID and the timestamp. So we have a genre name, a get set, and then we have a list of movies as a navigation property between the movie genre and the movies.
We're going to initialize that as a new list of Movie. Correct my typo here. All right, let's move that into it's own file. One other refactor we want to do is move entity base into the Base folder, get that out of our way. Okay, and then we need to add the navigation property. Now we have a genre property right now. We're going to keep that but we need to add in the navigation first.
Let's make it a required, we add in the foreign key. Public.int Movie Genre ID. And that's a get in the set. And then we're going to add in the movie genre, here we'll just call it Movie Genre. And to be very, very specific we will say the foreign key is the name of movie genre ID.
And then we need to add a using for that. So let's go ahead and do that right here. And correct my typo. Now what we can do, we'll get rid of these attributes because they aren't needed anymore. And this will be a convenience method. We're going to add the not mapped, because we don't need that to be a fuel to the database, and have it only return, so we can use a lander for that, movie genre.genre name, but we want to make sure that, for example during construction, when the movie genre might be null, that we put a safeguard in there to null call list.
There are our models.
Phil Japikse begins by showing how to install and update the .NET Core SDK. He reviews the functionality of the MVC 5 app, explains how to create necessary projects, and discusses migrating static content. Next, he demonstrates how to create a data access layer, complete the server-side migration, set up the HTTP pipeline, add custom items into the dependency injection container, leverage the new project configuration system, and migrate the controllers. Phil then introduces Tag Helpers—one the big new features in ASP.NET Core—and uses them to migrate and simplify the views. Phil also demonstrates how to create and use custom Tag Helpers. To wrap up, he covers working with view components, explaining what they are and why they're helpful. He walks through how to create the server-side view component code, and how to refactor your app and invoke the view component.
- Reviewing the MVC 5 application
- Creating the data access layer
- Adding and updating the models
- Updating the database
- Completing the server-side migration
- Configuring the HTTP pipeline
- Configuring and using dependency injection
- Migrating the views
- Creating view components