System.ComponentModel.DataAnnotations.Schema namespace conflict

1.5k Views Asked by At

I am using MVC 4, .net 4, and Entity Framework 6. My projects are building on my dev machine which has Visual Studio 2010 installed but on my build server I get a namespace conflict...

The type 'System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute' exists in both 'd:\Projects\tools\Apps\LAMS\packages\EntityFramework.6.0.2\lib\net40\EntityFramework.dll' and 'c:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.ComponentModel.DataAnnotations\v4.0_4.0.0.0__31bf3856ad364e35\System.ComponentModel.DataAnnotations.dll'

I'm unsure how to resolve this conflict and I do not understand why I do not receive the conflict on my dev machine.

Many thanks

1

There are 1 best solutions below

1
On BEST ANSWER

Problem

.NET 4.0 really shouldn't include System.ComponentModel.DataAnnotations.Schema so it's a little baffling that you'd get a reference conflict, but apparently .NET 4.5 overwrites 4.0 assemblies.

You can find more information in this post by Marc Gravell which states:

fact 1: 4.5 is an in-place over-the-top install on top of 4.0, in the GAC; once you have installed 4.5, 4.0 runs with the 4.5 assemblies

fact 2: if your build server doesn’t have the reference assemblies, it looks in the GAC – so… once you’ve installed 4.5, it will get 4.5 even if you asked (in your project) for 4.0

Solution

On your development machine, Visual Studio adds a different folder that is used for referencing .NET 4.0 assemblies, which is something like

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0

You can copy that folder over to your build machine, and it should work since MSBuild will reference these when building a project targeting 4.0.

On the other hand, since you have uninstalled .NET 4.5 on your build machine, maybe just reinstalling .NET 4.0 would work as well.

Also, see this Stack Overflow answer.