NuGet包提示“此源中不可用”但VS仍识别,无法编译Npgsql源码求助
I’ve run into exactly this kind of problem when debugging Npgsql.EntityFrameworkCore.PostgreSQL’s source code before—let’s break down what’s likely happening and how to fix it:
Common Causes & Fixes
1. Check if you’re missing solution dependencies
Npgsql’s EF Core provider relies on its core Npgsql library and other related projects. If you cloned only the Npgsql.EntityFrameworkCore.PostgreSQL repo without including its sibling projects in your solution, Visual Studio might try to fetch NuGet packages instead of using local source code.
- Open your solution file and confirm all required sub-projects (like the core
Npgsqlproject) are present. If not, check the repo’s README for instructions on cloning the full code suite or switching from package references to project references. - Inspect the
.csprojfile: Look for<ProjectReference>entries for core dependencies instead of<PackageReference>—if you see package references where local project references should be, that’s a sign you’re missing critical local source files.
2. Clear NuGet’s local cache
Visual Studio often caches package metadata, leading to inconsistencies where it claims the latest version is available but can’t locate the actual files. Here’s how to reset it:
- In VS: Go to
Tools > Options > NuGet Package Manager > Package Sourcesand confirm nuget.org (or any required private sources) are enabled. - Clear the cache: Go to
Tools > NuGet Package Manager > Manage NuGet Packages for Solution, click the gear icon in the top-right, and selectClear All NuGet Caches. - For a more thorough reset, open a command prompt and run:
dotnet nuget locals all --clear
3. Check version lock files
Many open-source projects (including Npgsql) use Directory.Packages.props or NuGet.Config at the solution root to lock package versions across all projects.
- Open these files and verify the version numbers match packages available on your configured NuGet sources. If the repo targets a pre-release version only available on a private feed (like a project-specific MyGet source), add that source to your NuGet settings.
4. Restore and rebuild properly
After cleaning the cache and verifying dependencies:
- Right-click your solution in Solution Explorer and select
Restore NuGet Packages. Wait for this to complete fully—don’t interrupt the process. - Try rebuilding the entire solution (not just a single project) via
Build > Rebuild Solution. If errors persist, use the command line for detailed output:
The detailed log will specify exactly which package is missing and why.dotnet restore dotnet build --verbosity detailed
5. Match your branch to stable package versions
If you pulled the main branch of the repo, it’s likely targeting an unreleased pre-release version that isn’t available on public NuGet. Switch to a stable branch (like v7.0.x or v8.0.x, matching your EF Core version) that aligns with publicly available packages, then repeat the restore and build steps.
内容的提问来源于stack exchange,提问作者user610217

