Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#EF必需属性未识别_C#_Entity Framework_Sqlite_Reflection_Attributes - Fatal编程技术网

C#EF必需属性未识别

C#EF必需属性未识别,c#,entity-framework,sqlite,reflection,attributes,C#,Entity Framework,Sqlite,Reflection,Attributes,我正在创建一个使用数据库(SQLite)的应用程序。我正在使用实体框架和ADO.NET与之交互。 我的应用程序中有一个seprate Models项目,其中包含我的所有数据库模型 现在,我想根据需要标记一些类属性,以反映数据库中的“notnull”选项。但是如果我从DataAnnotations名称空间添加[Required]属性,我会得到一个编译器错误,说它无法解析。 下面是我的班级的样子: using System.Collections.Generic; using System.Comp

我正在创建一个使用数据库(SQLite)的应用程序。我正在使用实体框架和ADO.NET与之交互。 我的应用程序中有一个seprate Models项目,其中包含我的所有数据库模型

现在,我想根据需要标记一些类属性,以反映数据库中的“notnull”选项。但是如果我从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; }
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
命名空间回流模型
{
公共类标签
{
公共标签()
{
this.Options=newhashset();
}
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共ICollection选项{get;set;}
}
}

我还在项目中添加了对EntityFramework.dll的引用。

您需要将其添加到使用块中

using System.ComponentModel.DataAnnotations.Schema;

您需要将其添加到using块中

using System.ComponentModel.DataAnnotations.Schema;
使用System.ComponentModel.DataAnnotations


如果它仍然无法工作,也许您应该将其添加到
引用中

是否添加了对System.ComponentModel.DataAnnotations.dll的引用?请尝试:[System.ComponentModel.DataAnnotations.Required],编译器将告诉您是否忘记引用汇编System.ComponentModel.DataAnnotations,。如果错误消失,那么您忘记使用System.ComponentModel.DataAnnotations;顺便问一下:编译器错误的确切文本是什么?哇。真不敢相信我竟然忘了。加上它,它就起作用了。感谢您快速而有用的回答:)