1. Getting Started
We are going to jump right into an example, as I think that is the best way to learn something. As we cover new topics, we explain them as we work on them. This is better than a general overview at the start of the chapter or section, because that wont mean much to you while you are reading it. I personally dislike it when books show you the wrong way of doing something and then show you how to do it correctly after you just spent five minutes typing in the wrong way, so Im not going to do that to you. However, I do explain why we are doing something and explain what would be wrong. Since this is a getting started type of book, all the examples work as written, although they may not be the best way of getting it done. As you get more comfortable with this technology and as your skills grow, youll come up with your own way of doing things. Im just here to help you started down the path to greatness.
For those of you who are like me and skipped the Introduction, you should be using the latest build of Visual Studio 2017 and at least .NET Framework 4.6.1. At the time of this writing, the latest build of Visual Studio was 15.3.3 with the .NET Framework build 4.7.02046. These build numbers can be found in the Visual Studio About window.
What Is .NET Core
Lets take a quick moment to cover something that some of you may be wonderingwhat is the difference between .NET and .NET Core? First off, .NET Core is cross platform, so if you want to run an application on Windows, Linux, or Mac, .NET Core is your tool. Due to its compact nature, .NET Core also gives better performance. The other nice thing is that you can always start with .NET Core and, if you find you need more features, you can switch to the full version of the .NET Framework. This is especially true if you are writing a service.
The application created here is used throughout the first section of this book and each section builds off the last, so you really cant skip around. With that being said, lets get started!
Setting Up Your Application
Follow these steps to set up the application:
Step 1: Create a new Visual C# Console App (.NET Core) Application in Visual studio called ComputerInventory . Again, make sure you are using at least .NET Framework 4.6.1 for your application.
Step 2: Open the NuGet package browser by clicking on Project Manage NuGet Packages (see Figure ).
Figure 1-1
Location of NuGet package manager
Step 3: Add the following packages:
Microsoft.EntityFrameworkcore.SqlServer v2.0.0
Microsoft.EntityFrameworkcore.Tools v2.0.0
Microsoft.EntityFrameworkcore.SqlServer.Design v2.0.0
Note
As of the writing of this book, only the preview packages were available. If you cant find version 2.0.0, check the preview packages.
Figure shows what it looks like when you search for a package. Once you have selected the package you want to install, just click on the Install button to the right and click I Accept for any prompts that come up.
Figure 1-2
NuGet package manager
That completes the basic setup of your application. Feel free to save your work before you continue.
Creating the Database and Tables (Entities)
If you have used Entity Framework 6.x or one of the other versions in the past, this next part will be a little new to you (or perhaps not). We are going to start with a code-first Entity Framework type of application, as that is somewhat easier when using EF Core. Later in the book, we use an application that is database-first, so you can see the difference. If you were to create an application based on an existing database, database-first is the choice youd probably use.
We are going to follow the model that is generally used by most people who design websites, so we need to create two folders in our application Models and Data . If you have never done this before, its simple. Just right-click on the ComputerInventory project in the Solution Explorer and select Add and then New Folder. Then change the name to Models . See Figure . Each of our eventual tables will have a corresponding class file in the Models folder. Ill take you step by step through the first one and then you should be able to create the remaining ones on your own (you just change the name of the class).
Figure 1-3
Adding a new folder
Lets create our first class file. Right-click on the Models folder, select Add, and then select Class (should be at the bottom of the list). Make sure that Class is selected and change the name to OperatingSys.cs . Figure shows you what it should look like when you are creating the new class file. We are using OperatingSys rather than OperatingSystem , as OperatingSystem is a reserved type in C# and wed have to put Models.OperatingSystem in our code each time we wanted to use it.
Figure 1-4
Creating a new class
Once you click on Add, OperatingSys.cs will be created and load for you to start working on it. Listing shows the code for this new class.
using System;
using System.Collections.Generic;
namespace ComputerInventory.Models {
public partial class OperatingSys {
public OperatingSys() {
Machine = new HashSet();
}
public int OperatingSysId { get; set; }
public string Name { get; set; }
public bool StillSupported { get; set; }
public ICollection Machine { get; set; }
}
}
Listing 1-1
OperatingSys.cs After You Changed It
The first thing you need to remember is to make the class public so it will be accessible throughout the application. What we have done here is created the basis for the first table in that database, which we will call OperatingSys . In your table, its a good idea to have an ID field that is normally a primary key, and we have done that with OperatingSysID . You should have two errors, both telling you the same thing, that the type or namespace name Machine could not be found. That is correct, as you havent added them yet, so you can ignore this for now.
Two things should hopefully jump out at you. We have created a constructor for our class and within that, we have created a new HashSet called Machine . You dont need to use a HashSet here, but you do need to use a collection and since that is the default for EF Core we are going to stick with it for our examples.
If you are more familiar with EF Core and have the time, I highly recommend looking at the other set types as there are cases in which using a HashSet isnt needed and there is a better fit. We then have our ICollection , which provides an interface to the Machine table. After all, one OS could have multiple machines, but one machine generally only has one OS (we arent going to handle multi-boot systems in this simple example).