In this section I migrate the models and ViewModel from EF6 to EF Core.
- [Instructor] Now that all of our projects are set up it is time to migrate the data access layer. We're going to start with the models from the SpyStoreModels project. So we want to copy the models from the original project and note that there are a few changes with model classes between EF 6 and EF Core. The table attiribute must specify the schema specifically as opposed to including it in the name of the table. Navigation properties are no longer marked virtual, and that is because lazy loading has not yet been implemented for EF Core, so there's no need to mark it virtual.
And collections are just the standard List of T instead of the ICollection of T. So here we have our project and we are just literally going to copy out of the directory and plop all that right into the SpyStoreModels project. You could, of course, right click add existing item in navigate, but this is much quicker. And we'll delete that original Class1 file that was brought over when we did the file New Project.
So starting with the Category model, we have to change it from store.categories, cause EF Core won't recognize that as a schema, and add in the schema name specifically, just like that. We also want to remove the virtual and then change the Icollection to a list, which means we can just do a new list of product.
And those are the only changes we need to make on the category. Save that. We'll move on to Product. Again we'll just change the table attribute to include the schema. Get rid of the virtual on the navigation property. And change the ICollection to a list, and instantiate it as a new list, instead of a HashSet.
And we'll move on to the shopping cart record. We should be very familiar by now. We'll add in the schema. We will scroll down and remove the virtual. The final change we make in the Models project: open up to view model, and there's a static method here that creates the view model from a shopping cart record.
When we get into the shopping cart repo you'll see how this was being used. With a new feature in EF Core we can create view models directly from SQL, so we don't need this method anymore. So we'll go ahead and delete that static method. And that completes migrating the models.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services