Can I implement Entity Framework 5 TPT on Windows XP?

点点圈 提交于 2019-12-02 07:19:39

First of all, remove the System.Data.Linq namespace and don't use the TableAttribute from there. It is for LINQ-to-SQL and not for Entity Framework.

Between version 4.1 and 5.0 of Entity Framework the Table attribute (among others) has been moved around a bit between namespaces and assemblies:

Depending on the version you can find the attribute at the follwing places:

Entity Framework 4.1 to 4.3 and target framework .NET 4.0

  • Add reference to EntityFramework.dll assembly to your project
  • Add using System.ComponentModel.DataAnnotations; to code file

Entity Framework 5.0 and target framework .NET 4.0 (This combination is also called Entity Framework 4.4)

  • Add reference to EntityFramework.dll assembly to your project
  • Add using System.ComponentModel.DataAnnotations.Schema; to code file

Entity Framework 5.0 and target framework .NET 4.5

  • Add reference to System.ComponentModel.DataAnnotations.dll assembly to your project
  • Add using System.ComponentModel.DataAnnotations.Schema; to code file

The TableAttribute has kept its old purpose and meaning in all versions. Especially it's the correct one to use for TPT.

In your situation where you want to support Windows XP but use EF 5.0 you need .NET 4.0 as target framework. To get the correct TableAttribute you have to do:

  • Add reference to EntityFramework.dll assembly to your project (most likely you have that already)
  • Check if this DLL (right mouse -> Properties) has version number 4.4. You can potentially have the wrong version (5.0) if you downloaded the Nuget package while your project targetted .NET 4.5. and you changed the framework later to .NET 4.0. In that case remove the reference to the current EntityFramework.dll and add a reference to the EntityFramework.dll assembly in the path packages\EntityFramework.5.0.0\lib\net40 under your solution folder
  • Add using System.ComponentModel.DataAnnotations.Schema; to code file

Start with the last of these three points, maybe it's your only problem that you have to append .Schema to the using directive.

The class you are trying to use is new to Entity Framework 5. You can't use Entity Framework 5 on XP because it requires .NET 4.5. The reason you are getting the errors is NuGet likely downloaded Entity Framework 4.4.

What the properties look like when you download it from NuGet from a .NET 4.0 project

What the properties look like when you download it from NuGet from a .NET 4.5 project

Your only two options are

  1. Don't use TableAttribute
  2. Don't support XP
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!