我正在创建一个使用数据库(SQLite)的应用程序。我正在使用实体框架和 ADO.NET 与之交互。 我的应用程序中有一个单独的模型项目,其中包含我的所有数据库模型。
现在我想根据需要标记一些类属性,以反映数据库中的“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 的引用。
您需要将其添加到您的 using 块中
using System.ComponentModel.DataAnnotations.Schema;
您需要将其添加到您的 using 块中
using System.ComponentModel.DataAnnotations;
如果它仍然不起作用,也许你应该将其添加到你的
References