In this section, all of the solutions and projects are created. After the projects are created, all packages are updated to the latest version.
- All right, enough talk. Let's start building things. We're going to start by creating the ASP.NET Core project and solution. This is very simple. Select Web or .NET Core under the Visual C# templates. Select ASP.NET Core Web Application, and make sure you pick the one that is for .NET Core because ASP.NET Core will also run on top of the full .NET framework. On the next screen, you can choose either the LTS or the current template, and we're going to pick the current 1.1 and then select the Web API template.
We'll go ahead and do this now. Let's select Create New Project here under the New Project section. .NET Core, ASP.NET Web Application .NET Core. Change the directory to one of your choosing. And let's name the project Spy Store API, and we'll name the solution the same. Go ahead and click OK.
Here we can choose between the LTS or the current, we'll stick with the current. Let's select Web API. We're not going to use the authentication or docker at this time, and say OK. We have to crate two more projects. One for the data access library and one for the models. In EF6, you could set both of as class libraries. EF Core requires an entry point to run migrations.
If a library has an entry point, that actually makes it a console app. There are some other ways you can work around that, but just to keep things simple, always make your project that will be holding the DB context a console app. And then the models project can just be a class library. Let's go ahead and take care of these two. I'm going to right-click on this solution, add New Project, still in .NET Core.
Console app will be the Spy StoreDAL, and we say OK. After that gets created, we then want to create one more. Right-click add New Project, class library under .NET Core, and we'll name this Spy StoreModels.
We just need to add some references and this has not changed. Right-click on the Web API project. Click add Reference. Under Projects, just check them like that. Now we also want to add a unit test project into this so we can test the data access layer. We already have a pretty sweet template in Visual Studio for using xUnit.
If you're not familiar with xUnit, the good folks who built nUnit, wanted a do over. Started a new project called xUnit. xUnit fully supports .NET Core. It fully supports the full .NET framework, and it really has become one of the most popular unit testing libraries. So let's add our test project. Same thing, right-click Solution, add New Project. xUnit Test Project.
We'll call this DAL test. I know, terrible names, but the three hardest things in programming, naming conventions and off by one errors just want to keep it consistent with what we're migrating from, so that name spaces don't become an issue. Finally, right-click on the new test project, add Reference, and we need it to the DAL and the Models. Click OK. Next we want to create the service test solution.
This is that separate solution that we saw in the full dot Framework world. To do that, we're going to start a new instance of Visual Studio, and we're going to create a new solution. Add an xUnit test project into there, and then we're going to add the existing Spy StoreDAL and Spy StoreModels projects into the solution. There are other ways we could do this. Probably the most common way is to package up those shared projects as NuGet packages and publish them to a local package store, but that adds complexity to what we're trying to show here, so I opted just to do the easy way and add those existing projects into the solution.
Create a new project. This time we're going to start with an xUnit project. Go to your favorite directory, and we're going to start at the same route level. We're going to call this project, Spy StoreService.Tests, and because my original sample did not use the consistent naming convention, we're going to make it match the old and that is, without the period.
And we say OK. Then we right-click add Existing Project this time. We'll start with the Models. Add that project in. We'll then add the data access layer. So add Existing Project, Data Access Layer.
Now that we have those projects, we have to add the references. So just like we did before, add Reference to both the DAL and the Models. Say OK. And make sure everything is saved. In any framework, ASP.NET, all of those libraries are a series of NuGet packages. Packages tend to update faster than the Visual Studio 2017 templates, so we want to make sure that we update those packages as soon as we create our projects and solutions.
Now we can update them a variety of ways. A command line NuGet package manager, or the package manager console. Let's go ahead and update everything. We're going to use the NuGet package manager GUI just because it's easier and that way, you can visually see what is going on. We'll start with the solution for the ServiceTests. Right-click, manage NuGet packages for solution.
And we look, and there's no updates. Fantastic. Now I just got a new update to Visual Studio, so that's probably why there aren't any package updates yet. Let's pull up the bigger solution, do the same thing. Right-click. Manage NuGet packages for solution. So we click on Updates, and we see there aren't any updates to be had. Fantastic.
- 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