Can I implement Entity Framework 5 TPT on Windows

2019-03-01 23:40发布

Examples of EF5 Table Per Type that I have found, such as this one use the [Table("tablename")] attribute to mark a class as defining a table.

When I add this attribute I get errors:

Error   1   The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)    E:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DomainClasses\Classes.cs    599 6   DomainClasses
Error   2   The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)    E:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DomainClasses\Classes.cs    599 6   DomainClasses

I have the line

 using System.ComponentModel.DataAnnotations;

in my namespace

And I am using framework 4 because I want the app to run on Windows XP.

[Update] I had a look at the link flagged as a possible duplicate here, and as a consequence added a reference to System.Data.Linq and a using System.Data.Linq

The error messages are now

 Error  1   The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)    E:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DomainClasses\Classes.cs    599 6   DomainClasses
Error   2   Using the generic type 'System.Data.Linq.Table<TEntity>' requires 1 type arguments  E:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DomainClasses\Classes.cs    599 6   DomainClasses

Importantly, I want my code to work on Windows XP , and the second answer to the possible duplicate requires framework 4.5

[Update] Code is as follows;

namespace SBD.Syrius.DomainClasses
{
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
 using System.Data.Linq;

 [Table("Product")]
 public class Product : LoggedEntity
 { 
    [Required]
    public string BuyUnitMeasure { get; set; }
    [Required]
    public Decimal BuyUnitQuantity { get; set; }
    [Required]
    public String Code { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public String SellUnitMeasure { get; set; }
    [Required]
    public Decimal SellUnitQuantity { get; set; }

    public virtual Template Template { get; set; }
    [Required]
    public string UnitMeasure { get; set; }


    public override string ToString()
    {
        return !string.IsNullOrEmpty(this.Name) ? this.Name : "Products";
    }

}

 public abstract class LoggedEntity
{
    public int Id { get; set; }
    public Guid RowId { get; set; }

    [ConcurrencyCheck]
    public int RowVersionId { get; set; }

    public int SourceSiteNumber { get; set; }

 }
}

[Update]

I corrected the using to be using System.Data.Linq.Mapping;

Now my error is

 System.Data.Linq.Mapping.TableAttribute' does not contain a constructor that takes 1 arguments  

[Update]

I also looked at the not accepted answer to the suggested duplicate question. Which is to use System.ComponentModel.DataAnnotations however this requires framework 4.5 and I don't think I can use that because it wont run on Windows XP, and I need to run on XP.

[update] Here is the screen in Manage Nuget Packages

Here is the reference in my project

I am developing on Windows 7 but the application needs to run on XP. I see the example I am trying to follow Here it is again uses Framework 4.1 Fluent

My question is Can I use TPT on EF5 to run on Windows XP? If so, how?

2条回答
爷的心禁止访问
2楼-- · 2019-03-02 00:25

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

enter image description here

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

enter image description here

Your only two options are

  1. Don't use TableAttribute
  2. Don't support XP
查看更多
淡お忘
3楼-- · 2019-03-02 00:36

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.

查看更多
登录 后发表回答