20

I am building an ASP.NET Web Forms web site using .NET 4.5.

The error ...

The type 'System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute' exists in both 'f:\Projects\web sites\RC1Iteration05\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll' and 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'

I have tried to alias the libraries using ...

csc /r:EF_DataAnnotations="f:\Projects\web sites\RC1Iteration05\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll" /r:CM_DataAnnotations="c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll"

but this only resulted in "No Source File specified" which is equally confusing since the source files were specified as directed (here & here).

I did notice that the error was referencing the EF dll in the net40 folder rather than the net45 folder. I figure if I used the net45 version the problem would resolve itself, however I do not know how to change that reference. I changed the "targetFramework" attribute to the EntityFramework package in the packages.config file, but that did not make any difference.

I am a bit stuck since both of the solutions did not seem to do anything.

I looked around and found a number of posts here where folks have dealt with similar issues but have received no responses. I am hoping that there is someone out there who can help!

Thanks

G

Gary O. Stenstrom
  • 2,284
  • 9
  • 38
  • 59
  • The `/r` switch only applies when you're actually compiling something on the command line. – SLaks Jul 22 '13 at 15:53
  • So I do NOT do command line at all. I thought that I WAS compiling a new alias library? If not then, what was trying to do? – Gary O. Stenstrom Jul 22 '13 at 16:31
  • No. Aliases simply allow you to refer to the referenced assembly using a different name in source files. – SLaks Jul 22 '13 at 16:36

2 Answers2

46

As you noticed, you're using the .Net 4.0 version of Entity Framework on .Net 4.5.
That won't work.

Re-install EF from NuGet and it should work fine.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I am using SQL Server CE 4.0 and installed a package that enabled EF to be used with CE (part of the problem)? I don't see it in the package list though. When I went to uninstall the EF package it failed due to a dependency issue (OAuth). In my experience I have found that the uninstalling and re-installing of packages tends to be a hit or miss practice, frequently destabilizing my project and requiring hours to re-assemble it or even having to recreate the project all over from scratch. Is there a way of re-installing the package that does not require me to "gut" the current references? – Gary O. Stenstrom Jul 22 '13 at 16:30
  • Try manually changing the reference to point to /net45. However, re-installing NuGet packages should not be a troublesome task (although you may want to restore `web.config` from source control afterwards). It sounds like you've messed up your local references. If you get rid of all references and re-install everything from NuGet only, you shouldn't have any trouble. – SLaks Jul 22 '13 at 16:36
  • I have tried changing the value in the package.config but that did not work. Is there somewhere else I should be looking to manually change the reference? – Gary O. Stenstrom Jul 22 '13 at 21:17
  • You need to change the reference in the project. – SLaks Jul 22 '13 at 21:24
  • Thanks SLaks ... I put my faith in you and rei-nstalled the NuGet package and everything appears to have gone smoothly. Thank you!! – Gary O. Stenstrom Jul 22 '13 at 21:28
  • I had the same issue with EF 6.0.1. An upgrade to 6.0.2 fixed the problem! – arni Mar 14 '14 at 09:27
  • I had a couple of hiccups when I was creating my Model, so I suspect that - that is why it didn't work the first time. Crashed the first time and then I had to do it again after deleting what was there. The second time the EF reference was an older one from a different project. – dyslexicanaboko Sep 12 '16 at 20:07
7

The other options didn't work for me. What did work was going into \packages\EntityFramework.6.1.3\lib\ and deleting the net40 directory and then doing a rebuild all.

EfficionDave
  • 2,736
  • 3
  • 31
  • 38