Sub topic 2
Setting Up a Project with EF Core and SQLite
In this chapter you build the empty project that the rest of the tutorial uses. You create a console app, add the EF Core packages, and install the command-line tool that builds the database. You won't write any database code yet. The goal is a project that compiles and has every piece in place.
What You Need First
You need the .NET SDK installed. The SDK (software development kit) is the set of tools that builds and runs .NET programs. To check that you have it, open a terminal and run:
dotnet --versionIf a version number prints, you are ready. If you see "command not found," install the .NET SDK from Microsoft's download page, then open a new terminal.
The Pieces You Will Install
EF Core is split into separate packages. A package is a bundle of ready-made code that you add to your project. NuGet is the online store that .NET downloads packages from. You only add the packages you need.
| Piece | What it is | Why you need it |
|---|---|---|
| Microsoft.EntityFrameworkCore.Sqlite | The SQLite provider package | It lets EF Core talk to SQLite. It also brings in the core EF Core library. |
| Microsoft.EntityFrameworkCore.Design | The design-time package | It lets the command-line tool read your code to create migrations. |
| dotnet-ef | A command-line tool | It runs commands such as `dotnet ef migrations add`. |
A provider is a package that translates EF Core's work into the SQL dialect of one database. SQL Server, PostgreSQL, and SQLite each have their own provider. Your C# code stays mostly the same when you switch providers.
SQLite is a small database engine that stores the whole database in one file on disk. It needs no server and no password, so it suits learning well. Later you can swap in another provider.
Step-by-Step Setup
Run these commands one at a time in a terminal:
dotnet new console -o EfCoreDemo
cd EfCoreDemo
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet tool install --global dotnet-efEach line does the following:
dotnet new console -o EfCoreDemocreates a new console app in a folder namedEfCoreDemo. A console app is a program that runs in the terminal.cd EfCoreDemomoves you into that folder. The commands after this one must run inside the project folder.- The first
dotnet add packagedownloads the SQLite provider and records it in your project file. - The second
dotnet add packageadds the Design package. dotnet tool install --global dotnet-efinstalls the EF Core tool for your whole user account. "Global" means you can use it from any folder.
Look Inside the Project File
The project file is EfCoreDemo.csproj. It is an XML file that lists the settings and packages of your project. After the steps above, it looks roughly like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.0" />
</ItemGroup>
</Project>Your version numbers may be different, and that is fine. Each PackageReference line is one package. The PrivateAssets setting on the Design package means it is used only while you develop. It is not shipped with your finished app.
You can also list the packages from the terminal:
dotnet list packageCheck That Everything Works
Run these two checks. First, confirm the tool is installed:
dotnet ef --versionYou should see a short logo and a version number. Second, confirm that the project can use EF Core. Replace everything in Program.cs with this:
using Microsoft.EntityFrameworkCore;
var version = typeof(DbContext).Assembly.GetName().Version;
Console.WriteLine($"EF Core is ready. Version: {version}");Then run the program:
dotnet runIf the message prints, the packages are installed correctly. DbContext is the main EF Core class. The chapter after next covers it in detail. Here you only use it to prove the library is available.
Keeping Versions in Step
The dotnet-ef tool and the EF Core packages each have a version number. They should share the same major version. The major version is the first number, such as the 10 in 10.0.0. If the tool is older than the packages, you may see a warning or strange errors when you create migrations.
To update the tool to the newest version, run:
dotnet tool update --global dotnet-efTo make all EF Core packages in a project match, give each one the same version:
dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 10.0.0
dotnet add package Microsoft.EntityFrameworkCore.Design --version 10.0.0Option: A Local Tool Instead of a Global One
A global tool is shared by every project on your machine. On a team, people may have different versions installed. A local tool is installed for one folder and is recorded in a file named a tool manifest. Everyone who clones the project gets the same tool version.
dotnet new tool-manifest
dotnet tool install dotnet-ef
dotnet tool restoreThe first command creates the manifest file at .config/dotnet-tools.json. The second installs dotnet-ef into it. The third, which a teammate runs after cloning, installs every tool listed in the manifest. You still type dotnet ef to use it. For this tutorial, either the global or the local option works.
Common Problems and Fixes
| What you see | Likely cause | Fix |
|---|---|---|
| `dotnet ef` says "Could not execute because the specified command or file was not found" | The global tools folder is not on your PATH, or the tool isn't installed | Install the tool, then add `~/.dotnet/tools` (macOS/Linux) or `%USERPROFILE%\.dotnet\tools` (Windows) to PATH and open a new terminal |
| "Tool 'dotnet-ef' is already installed" | You installed it before | Run `dotnet tool update --global dotnet-ef` instead |
| "Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design" | The Design package is missing | Run `dotnet add package Microsoft.EntityFrameworkCore.Design` |
| A warning that the tool version is older than the runtime | The tool and packages are out of step | Update the tool to the same major version |
| "Couldn't find a project to run" | You are in the wrong folder | `cd` into the folder that holds `EfCoreDemo.csproj` |
PATH is the list of folders your terminal searches when you type a command name.
What You Have Now
You now have a console project with the EF Core SQLite provider and the Design package, plus the dotnet-ef tool. No database file exists yet. In the next chapters you will write entity classes and a DbContext, then use dotnet-ef to create blogging.db.
Exercise
You start a second project called ShopApp on the same computer. Which of these steps do you need to repeat there: adding the Sqlite package, adding the Design package, or installing the global dotnet-ef tool? Explain why.
Show answer
Repeat the two dotnet add package steps. Packages belong to a single project and are listed in that project's .csproj file. You don't need to install the global dotnet-ef tool again because it is installed once for your user account and works in every folder.
Exercise
You run dotnet ef --version and get "command or file was not found," but dotnet tool list --global shows dotnet-ef in the list. What is the most likely problem, and how do you fix it?
Show answer
The tool is installed, but its folder isn't on your PATH. Add the .NET tools folder (~/.dotnet/tools on macOS/Linux, %USERPROFILE%\.dotnet\tools on Windows) to PATH, then open a new terminal and try again.
Exercise
Why does the Design package have PrivateAssets set to "all" in the project file, while the Sqlite package does not?
Show answer
The Design package is only needed while you develop, so the dotnet-ef tool can read your code and create migrations. It is not part of the running app. The Sqlite provider is needed at runtime, because your app uses it every time it reads from or writes to the database.