0
votes

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.

2
You'd probably need to remove using System.ComponentModel.DataAnnotations; from your code. - Ivan Stoev
Hi Ivan. I tried that but I got this error:The type or namespace name 'KeyAttribute' could not be found (are you missing a using directive or an assembly reference?) - M Hilmi Koca
Sorry, disregard my previous comment. Actually in my EF6.1.3 test project I have using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; And both attributes are coming from Assembly System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Looks like something is wrong with EntityFramework assembly in your scenario, have no idea how it can be resolved. Good luck. - Ivan Stoev
Thanks for advice. But I upgraded depended projects :) - M Hilmi Koca

2 Answers

5
votes

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.

1
votes

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