Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing NuGet packages and references once and for all

Tags:

Windows 8.1 Enterprise x64, Visual Studio 2015, MVC 5, EF 6, VS Online using git

I'm a relatively new developer with Visual Studio (about nine months), and ever since I started I've had an incredibly difficult time with references and NuGet packages. All of my references were working correctly on Friday when I shut my computer down for the weekend. I didn't touch the computer at all the entire weekend, now I've booted up today and I have a ton of reference errors:

list of errors

(There are 6,262 errors total but I obviously can't screenshot the entire thing.)

Like I said, this sort of problem occurs very often, and it's incredibly frustrating. Things I've tried:

  • Ensure all references throwing the errors actually are referenced
  • Un/reinstall the packages throwing the errors in NuGet
  • Completely delete the contents of the packages folder and let NuGet restore them (all reinstalled, views have the same errors as before)
  • Ensure every single reference is set to Copy Local
  • Close the solution, delete all bin and obj folders, reboot computer, clean, rebuild
  • Ensured all necessary references are in the relevant web.config (either inside Views or at the root)
  • Check for NuGet option to restore missing packages (did not appear, all packages are in my local packages folder)
  • Other things on SO that I'm sure I've forgotten to list here

I'm at my wits' end with these packages and references. It's a different fix every time, and this time I can't figure it out. Am I missing some obvious fix, something I overlooked? Is there a way to somehow take a backup when this is working and restore it whenever things break? Any ideas whatsoever, whether helping with the current problem or for fixing the underlying problem, would be very much appreciated.

Additional note

The problem is NOT only with views--controllers are also throwing errors. Specifically:

The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?)

(It literally says Controller, that's not something I changed for privacy or whatnot.)

These are thrown despite the fact that I have using statements for all of the necessary namespaces. The using statements themselves work properly, but the error is thrown in the code. The automatic fix suggested is to manually reference everything: for example, var sb = new StringBuilder(); wants me to change the line to var sb = new System.Text.StringBuilder();. Testing that fix does not correct the problem, the same error is thrown but on System instead of StringBuilder.

Additional requested information

  • I use git with VS Team Services (but packages are ignored with .gitignore).
  • I and one other person work on this, but the other person hasn't touched it at all (not even pulling from the remote repo) in several weeks.
  • References in the .csproj file are in the format ..\..\..\packages\ (correct for the location relative to the .csproj file).
  • Targeting .NET 4.6 (always have been, that's not new)
  • All references resolve, no exclamation marks in the references list.
like image 568
vaindil Avatar asked Dec 14 '15 14:12

vaindil


People also ask

How do I update NuGet package references?

Update a package. In Solution Explorer, right-click either References or the desired project, and select Manage NuGet Packages.... (In web site projects, right-click the Bin folder.) Select the Updates tab to see packages that have available updates from the selected package sources.


2 Answers

New Projects

There is one single thing you need to do in order for packages to work correctly with git:

When you create a repository, make sure you add a .gitignore tailored for Visual Studio development. You can search google for such a file or take it from here.

This will make sure you don't commit anything that will cause problems later.

This should solve a lot of the problems that usually happen when you check in packages to a code repository. You can add/remove packages and upgrade packages and also be able to clone the repository to a new machine and packages will be restored automatically.

Existing Projects

This is good for new projects. If you already have a project with a great big mess, then it is very hard to fix it since Visual Studio keeps package versions in several places - packages.config and app.config (and web.config where relevant).

Option 1 - Fix the current project

You can remove all packages (remove all the references from all projects and delete the package contents).

Make sure that the packages no longer appear in any file (e.g. packages.config or app.config). Now add the .gitignore and then start adding the packages back.

Option 2 - Create a new project

If it doesn't work, start a new project, add the .gitignore, transfer all the code (only your code) and install the packages.

like image 174
daramasala Avatar answered Oct 01 '22 04:10

daramasala


I'm not quite familiar with how Git works in VS, but I recall one time recently when using TFS that unbinding the solution from source control, fixing all packages, cleaning and re-building before binding it again solved a bunch of issues regarding NuGet references.

I apologize if Git works way different within VS.

like image 45
Varuuna Avatar answered Oct 01 '22 03:10

Varuuna