2017-03-23 50 views
1

我正在创建一个使用数据库(SQLite)的应用程序。我正在使用实体框架和ADO.NET与它进行交互。 我在我的应用程序中包含了所有数据库模型中的单独模型项目。C#EF必需属性不被识别

现在我想标记我的一些类属性,以反映我的数据库中的“NOT NULL”选项。但是,如果我从DataAnnotations命名空间添加[Required]属性,我会收到一个编译器错误,说它无法解析。 这里是我的课怎么样子:

using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

namespace ReflowModels 
{ 
public class Tag 
{ 
    public Tag() 
    { 
     this.Options = new HashSet<Option>(); 
    } 
    public int Id { get; set; } 

    [Required] 
    public string Name { get; set; } 
    public ICollection<Option> Options { get; set; } 
} 
} 

我也曾在我的项目加入到EntityFramework.dll参考。

+1

你添加引用添加到您的使用块System.ComponentModel.DataAnnotations.dll? –

+1

尝试:[System.ComponentModel.DataAnnotations.Required],如果您忘记引用Assembly System.ComponentModel.DataAnnotations,则编译器会告诉您。如果错误消失,那么你忘记使用System.ComponentModel.DataAnnotations;顺便说一下:编译器错误的确切文本是什么? –

+0

哇。不能相信我忘了那个。添加它,它工作。感谢您的快速和有用的答案:) – Kobek

回答

1

你需要这个

using System.ComponentModel.DataAnnotations.Schema;