Add a reference to System.Core when it's already referenced by the build system

After doing some research, all I could find were previous SO tickets and some blogs, and ultimately they all ended up using the same work-around you did. Then I found a bug report that someone filed on Microsoft Connect in 2011 (now only available on the Wayback Machine, or in a more readable format on my blog), which was eventually answered a year or so later by Chuck England, a MSFT employee.

TL;DR - It was a conscious decision the team made due to the multi-targeting feature added to VS2008 (being able to change a project's .NET Framework target).

Since the targeted framework requires "System.Core", and removing it would prevent a build (as far as I can understand Chuck's answer), they decided to force a reference to it even if you don't actually see it in the list of references.

A bit of a "safety" hack, you might say, with an undesirable side-effect that it throws that error when you try to add it to the references again, and you end up needing to edit the project file directly.


Microsoft Connect was retired in 2018, but here's the relevant parts of the original thread:

Yes, the experience is not great. Since System.Core is required, you should never remove it. We fixed this by adding it for you even if you remove the reference. [A]s the message you saw indicates, System.Core is implicitly referenced. So, the fact that you have removed it, other than physically removing a line from the project file, has not changed the build in any way.

There are legitimate scenarios where you might want to be able to do this, but it is a very edgy corner case. However, we should blindly ignore the fact that when you add it back an error is generated. A lot of this was imposed on us by previous versions which did not understand multi-targeting, and simply did not get cleaned up.

[Y]ou can manually add it back by right-clicking on the project node and selecting Unload. Right-click on the project node again and select Edit. In the editor, copy another reference line (for example, the one for "System") and paste it below the original reference inside the same ItemGroup. Change the reference name to "System.Core". Right-click on the project node and select Reload. Choose "yes" for question to save and reload.

VS2008 did not handle multi-targeting correctly. It would allow you to build things that were not legitimate. With VS2010, we have tried very hard to make sure that if it builds for the target framework, then it will run on the target framework. I can't say I know of any places where this is not true.

We can't go back to the VS2008 way, as it does not understand multi-targeting. Nor did it understand true "profiles", like the Client Profile of the .NET 4.0 framework. In fact, the issue you are seeing is because the VS2008 system was upgraded to handle multi-targeting, and it is the new set of rules that are rejecting the reference.

In this case, we build even though you removed a reference. You might say, but it should not have built. But, it will run on the targeted framework, and hence should have built. The fact that we have added a "required" refererence for you is somewhat of a broken experience in how it was developed.

We really just did not catch this early enough, and with a really solid understanding of how we could fix it to get a fix prior to our release. But, the fact that you should always reference, and hence never remove "System.Core", made this a Won't Fix, since this is not something that 99% of customers would ever do.


Building on @Arthur's answer, I have found two things resolved it for me:

Add to .csproj file, the missing line:

<Reference Include="System.Core" />

Then, according to a forums.asp.net answer, add the System.Core assembly reference in main web.config (not the one under Views):

  <system.web>
    <compilation debug="true" targetFramework="4.7.1">
      <assemblies>
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.7.1" />
    <!-- ... ... -->
  </system.web>

Then:

1) Ensure the version of the .NET framework for the compilation property in web.config is the same as that specified in the project properties.

2) Ensure the version of the MVC assembly in the views folder web.config is the same as the MVC assembly you are using in your project.

3) Restart Visual Studio (2017 here).

Hope this helps.


Recovered by manually adding these lines to the .csproj file:

<Reference Include="System" />
<Reference Include="System.Core" />