M Hilmi Koca
M Hilmi Koca

Reputation: 161

C# The type 'ForeignKeyAttribute' exists in both EntityFramework and System.ComponentModel.DataAnnotations

I upgraded project from .net 4.0 to .net 4.5.1.

and I upgraded EntityFramework from 4.3.1 to 6.1.3.

But EntityFramework based on .net 4.0 (\packages\EntityFramework.6.1.3\lib\net40\EntityFramework) because another depended project running .net 4.0.

When I build, I got this error:

The type 'ForeignKeyAttribute' exists in both 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

Thanks in advance.

Upvotes: 2

Views: 4856

Answers (2)

gregsonian
gregsonian

Reputation: 1226

It may not be enough to simply remove the DataAnnotations 4.0.0 references from the project. Even though the references to Entity Framework in your packages.config may be updated with the correct version the targetFramework value may still influence how the code gets compiled.

I was upgrading an ASP.NET MVC 3 application that was originally targeting Framework 4.0. I first upgraded the NuGet packages, which included EntityFramework 4.x. Later I learned I needed a newer .NET Framework version. After choosing to upgrade each project to .NET Framework 4.7.1, I started running into CS0433 errors. Upgrading the Framework version after upgrading the NuGet packages appears to leave the targetFramework unchanged.

To solve this problem, I manually updated each project's packages.config file with new targetFramework values from net40 to net471. To make these updated values take effect, from the NuGet Package Manager Console I ran Update-Package -reinstall. Now the DataAnnotations included in 'EntityFramework 6.2.0' are used instead of the externally-available ones in 4.0.0.

Upvotes: 9

Roger Willcocks
Roger Willcocks

Reputation: 1667

Entity Framework 6 includes a lot of stuff from the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespaces. As far as I can see, the fix is to remove the projects Reference to System.ComponentModel.DataAnnotations

Upvotes: 2

Related Questions