【问题标题】:How to upgrade DataAnnotations for Entity Framework 5 Code First如何升级 Entity Framework 5 Code First 的 DataAnnotations
【发布时间】:2013-09-12 22:05:37
【问题描述】:

我正在尝试从 EF 4.3.1 升级到 EF 5,并且还从 .Net 4 更改为 .Net 4.5。这是一个给我带来麻烦的类的示例:

using System.ComponentModel.DataAnnotations;

public class MyClass
{
    [Key, Column(Order = 0)]
    public int CompositeKey1Id { get; set; }

    [Key, Column(Order = 1)]
    public int CompositeKey2Id { get; set; }
}

首先我收到错误Cannot resolve symbol 'Column'

所以我添加了 using System.ComponentModel.DataAnnotations.Schema;,因为 ColumnAttribute 已移入 Schema 命名空间。现在我收到Ambiguous reference 错误,因为 EntityFramework.dll 和 System.ComponentModel.DataAnnotations.dll 中都存在 ColumnAttribute。

所以我尝试删除 System.ComponentModel.DataAnnotations.dll 作为参考,现在我得到 Cannot resolve symbol 'Key',因为 KeyAttribute 在那个 dll 中,但不在 EntityFramework.dll 中。

除非在 EF5 中不再需要 KeyAttribute,否则我必须降级 到 .Net 4 才能编译此代码。这不可能吧?我在这里错过了什么?

【问题讨论】:

    标签: c# .net entity-framework ef-code-first entity-framework-5


    【解决方案1】:

    您需要卸载 EF,然后将您的项目重新定位到 4.5,然后安装 EF。如果您首先重新安装 EF,您最终将获得 EF5 for .NET Framework 4(程序集版本 4.4.0.0),其中包含数据注释,因为它们不在 .NET Framework 4 中,并且数据注释来自 System.Data.ComponentModel.DataAnnotations。在 .NET Framework 4.5 中将数据注释移动到的 dll。在 .NET Framework 4.5 上,您希望 EF5 用于 .NET Framework 4.5(程序集版本 5.0.0.0),这应该可以解决问题。如果您已经重定向项目,只需卸载并重新安装 EF。

    【讨论】:

    • 谢谢,我在你回答的时候就知道了。
    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多