15

我确定我在这里遗漏了一些简单的东西。我正在尝试遵循 Code First Entity Framework 教程,该教程告诉我使用一些数据注释。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace Model
{
    public class Destination
    {
        public int DestinationId { get; set; }

        [Required]
        public string Name { get; set; }
        public string Country { get; set; }
        [MaxLength(500)]
        public string Description { get; set; }

        [Column(TypeName="image")]
        public byte Photo { get; set; }

        public List<Lodging> Lodgings { get; set; }
    }
}

编译器对前两个注释没有任何问题,但它似乎不喜欢:[Column(TypeName="image")].

错误:

  • 找不到类型或命名空间名称“列”。

  • 找不到类型或命名空间名称“ColumnAttribute”。

我正在使用 Visual Studio 2012 和实体框架 5。

有什么建议么?

4

2 回答 2

33

Entity Framework 4.3.1中,在namspaceColumnAttribute中定义,在. 因此,如果您有对该 dll 的引用和对该命名空间的 using 语句,则应该没问题。System.ComponentModel.DataAnnotationsEntityFramework.dll

Entity Framework 5中,它位于System.ComponentModel.DataAnnotations.Schemanamspace 中,因此您需要在您的类中添加对它的引用。

using System.ComponentModel.DataAnnotations.Schema;

您可以在此处阅读有关它的更多详细信息。

于 2012-10-06T10:00:01.320 回答
1

我有正确的using陈述......

尽管有正确的using陈述,我还是遇到了这个问题。

就我而言,我的项目是由 dotPeek 在反编译 dll 后生成的(原始源代码丢失了)。

dotPeek 创建项目时引用了 EntityFramework.dll 的副本,该副本位于某个文件夹中,不受 NuGet 管理。

对我有用的是删除对 EntityFramework 的引用,并使用 NuGet 控制台重新添加它。

于 2018-11-07T02:17:53.143 回答