This course uses two .NET Core projects, one for the Entity Framework Core code, and one for the xUnit tests. Phil uses xUnit to demonstrate the features of EF Core to remove the need for a user interface. This section covers creating the projects, adding the NuGet packages, and the prebuild classes from the downloadable code.
- [Instructor] All right, now that we've covered the theory of the course, let's dive in and create and configure the projects we'll be using. The first thing we want to do is create the main project and we're going to add a .NET Core console application. We'll go ahead and name it DataAccessLayer. Now, it needs to be a console app because the EF Core command line interface tooling requires an application entry point to execute. If we were building an ASP .NET Core website, or service, we could use the ASP .NET Core project as the entry point, but we are just going to have two projects in here, the DataAccessLayer containing all the Entity framework code, and a test class to drive the DataAccessLayer.
So, bit of a long explanation, but, the .NET Core CLI tooling requires an entry point, therefore we need to have a console application. Now, if you wanted to use the PowerShell commands and not the CLI tooling, then you could have made that a class library. We're going to add in the following NuGet packages, and that's EntityFrameworkCore, Core.Design and Core.Relational, and then we're going to add in our data provider, which is EntityFrameworkCore.SqlServer.
We're also going to add in Microsoft Extensions Logging Console, and that allows us to do some logging with an ILogger, and we'll see how to do that. And if you wanted to use the PowerShell command instead of the EFCore Command Line Interface tooling, you could add in the Microsoft.EntityFrameworkCore.Tools package. So let's go ahead and create this. So let's go New Project.
And we want .NET Core Console Application, and we're going to call this the data access layer. And let's put it in the correct directory. So we'll go to Lynda, EFCore, Essentials, Course1, and we'll make a new folder. And select Folder, and the solution name can stay, GetAccessLayer, that's fine.
You do have the option of creating a new Get repository here, we're not going to do that, but you can if you want to put all your code in Get. I already have one created for my Lynda code. So we just say OK. Visual C returns a little bit. Now we need to add in our new Get packages. There's to ways we can do that. One is simply manage NuGet packages. Browse, type in the name that we want.
Microsoft.EntityFrameworkCore. Select it, and install. It's going to prompt you to install all the supporting packages as well as accept the license agreement, and we're done. Now, to add in all of the others, we could do the same process, or something new in Visual Studio 2017 with .NET Core Projects, I can go here and say Edit Data Access Layer csproj.
Now, if I want to add in additional packages at this point, I can literally just paste them in and they will be added once I save the project file. If I go down to the package manager console, shrink that down a little bit, that's way too big. There we go. I can do dotnet restore and it'll restore the packages into the project. All right. Now, we need to add in the EF Core CLI tooling, and we have to do that in a separate item group with a .NET CLI tool reference as opposed to a package reference.
Visual Studio does not yet support adding this through the IDE, so we just need to edit the project as we just did. And what we want to do is edit the project file and paste into its own item group DotNetCliToolReference Include Microsoft.EntityFrameworkCore.Tools.DotNet. And the current version is 2.01. One thing I like to do is just to make sure I've got the right version is I'll go out here and type in Tools.DotNet.
And it is indeed version 2.01. If I tried to install it through NuGet Package Manager, I would get an error. All right, so the next thing we want to do is create the test project. Now, I'm using tests to drive out all the code samples. This is a data access layer, there isn't a UI, and instead of doing something through Console or WPF or a web app, which would just kind of cloud what I'm trying to show, we're going to do it all through unit tests.
So we're going to add another .NET Core project, this time a class library, and we'll name it DataAccessLayerTests. Add a reference to DataAccessLayer, and then we're going to add in Xunit and xunit.runner.visualstudio. Now, Xunit is a new testing framework, it was one of the first ones to support .NET Core. And we'll talk more about that framework later. The xunit.runner.visualstudio allows you to run the test through Visual Studio. If you'd like to run them from the console, you can add the console package as well, and then we also have to add in support for Entity Framework.
Now, we don't need all the design packages, we just need to have the core package, no pun intended, as well as the SQL Server package. So let's add the new project. Right-click, Add New Project. This will be a class library. And we'll call it Data Access Layer Test. And say OK.
And to save time, I am going to right-click on the project name, edit this csproj file, and then we will just add in the references that we need. I can also, through the project file, add the reference to the other project, but it's easy enough to add reference to the data access layer.
Okay, there's one watch out, and I just demonstrated it. Not intentionally, but we'll go ahead and talk about it anyway. I didn't save the project. So let me save it this time, and now when I run dotnet restore, it restores them and saved it. So I got a little ahead of myself, cut and pasted a little too much. I do like going into the project files and editing the references there. It's a little quicker than using NuGet Package Manager, however, you can shoot yourself in the foot, as I demonstrated there.
Now, what I have done for you is I've taken all of the tests that we will be writing and stubbed them out. This saves you watching me typing and saves you typing as well. So we want to go into the PrebuiltClasses directory. In there you'll see a Tests directory, and copy all of those directories and files into the DataAccessLayerTests project. So here I've got my prebuilt classes, tests, and we're going to copy all of this into this new project right here.
Paste, and what we will find is when we go back into the project, they all show up. And this is one of the beauties of .NET Core is we no longer have to specify all the files in the project file, it just looks for everything that's in the directory. So we'll go ahead and delete Class1.cs, warning us it will be deleted permanently, we're totally fine with that. And we say OK. Just to be safe, I'll go ahead and save it.
Don't really need to. And that is all it takes to build out the initial projects. So next we're going to reverse engineer the AdventureWorks database into our DataAccessLayer project.
- Entity Framework Core components and projects
- Working with scaffolded files
- Testing with xUnit
- Viewing generated SQL
- Composing queries
- Sorting and filtering results
- Working with aggregates
- Loading related data
- Logging and tracking
- Mapping functions
- Generics and delegates
- Checking concurrency
- Resiliency and transactions