NuGet Packages and Dependency Management
Level: Beginner to Intermediate
- What NuGet is and how it works
- Installing packages
- Managing package versions
- Dependency resolution
- nuget.org and package sources
- Semantic versioning
- Updating packages
What is NuGet?
NuGet is the package manager for .NET.
Like:
- npm (JavaScript)
- pip (Python)
- Maven (Java)
It downloads libraries and manages versions.
Install Package
dotnet add package Microsoft.EntityFrameworkCore
This:
- Finds package on nuget.org
- Downloads latest version
- Adds to .csproj
- Downloads dependencies automatically
Install Specific Version
dotnet add package Microsoft.EntityFrameworkCore --version 8.0.0
Search for Packages
Example: Search "json" — see 1000+ results (json parsing libraries).
Or via CLI:
dotnet package search EntityFramework
Common Packages for School Management API
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
dotnet add package AutoMapper
dotnet add package FluentValidation
Version Numbers - Semantic Versioning
Format: MAJOR.MINOR.PATCH
Example: 8.0.0
- MAJOR (8) — Breaking changes
- MINOR (0) — New features, backward compatible
- PATCH (0) — Bug fixes only
Update Packages
dotnet add package Microsoft.EntityFrameworkCore
Using this again auto-updates to latest.
Or specific:
dotnet add package Microsoft.EntityFrameworkCore --version 8.1.0
Remove Package
dotnet remove package Microsoft.EntityFrameworkCore
List Installed Packages
dotnet list package
Shows all packages and versions.
Dependency Resolution
When you install Entity Framework Core:
Microsoft.EntityFrameworkCore 8.0.0
└─ Depends on:
├─ System.Collections.Immutable (version X)
├─ System.Reflection.Emit (version Y)
└─ (10 more dependencies)
NuGet automatically installs dependencies.
Transitive dependencies — packages needed by packages you installed.
Lock File (packages.lock.json)
Controls exact versions installed.
Generate:
dotnet restore --locked-mode
Ensures everyone uses same versions (production stability).
Local NuGet Packages
Create local package:
dotnet pack
Creates .nupkg file (ZIP of your library).
Share locally:
dotnet add package MyLibrary --source ../path/to/packages
Pre-release Packages
Include preview versions:
dotnet add package EntityFrameworkCore --prerelease
Use for testing new features.
Package Sources
Default: https://api.nuget.org/v3/index.json
Add custom source:
dotnet nuget add source https://mycustomserver.com/nuget/
Troubleshooting Package Issues
Package not found:
dotnet package search MyPackage
Version conflict: NuGet resolves automatically. If conflict, specify version:
dotnet add package MyPackage --version 1.0.0
Clear cache:
dotnet nuget locals all --clear
Q: How does NuGet dependency management work and why is it important?
Good Answer: "NuGet is .NET's package manager. When I install a package like Entity Framework Core with dotnet add package Microsoft.EntityFrameworkCore, NuGet downloads it from nuget.org and adds it to my csproj file with a version number. Entity Framework Core itself depends on other packages (transitive dependencies), and NuGet automatically resolves and installs all dependencies with compatible versions. This is important because it avoids version conflicts and allows code reuse. Rather than writing my own database access layer, I can install proven libraries. Using semantic versioning (MAJOR.MINOR.PATCH) helps ensure compatibility - I can safely update patch versions knowing they won't break my code."
Use ChatGPT, Claude, or Copilot to go deeper on NuGet packages and dependency management. Try these prompts:
"How do I install and update NuGet packages?""What is semantic versioning and why does it matter?""What are transitive dependencies?""How do I find and use popular .NET packages?"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.